-1

how to use (String [] [] args) in java and write program of 2d array.

yasitha
  • 709
  • 2
  • 6
  • 12
  • possible duplicate of [Why is String\[\] args required in Java?](http://stackoverflow.com/questions/1672083/why-is-string-args-required-in-java) – Josh Lee Feb 01 '11 at 07:55
  • 2
    Is it a method that takes `args` as a parameter? – dacwe Feb 01 '11 at 07:56
  • 1
    @jleedev, I don't think it's a duplicate. String[] args has nothing to do with multi-dimensional arrays, and the name "args" is probably just a coincidence. Still, the question is too vague, what exactly is unclear to you, yasitha? A multi-dimensional array is just an array of arrays, so it's used exactly as a single-dimensional one. – Sergei Tachenov Feb 01 '11 at 07:59
  • @Sergey Tachenov: Yes, but it was closed because this is ambiguos, vague, incomplete. With more information it might be a duplicate of http://stackoverflow.com/questions/1067073/java-multidimensional-array –  Feb 01 '11 at 17:48

5 Answers5

3

String[][] is an array of arrays of Strings. Sometimes that's called a two-dimensional array.

An array of X can be created using new X[] { instanceOfX1, instanceOfX2, instanceOfX3 }.

Since X in this case is String[] you could create a String[][] like this:

new String[][] { new String[] { "a", "b" }, new String[] { "x", "y" }, new String[] { } }

Note that the inner arrays don't need to be of the same size (this is called a jagged array).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1

This is a example program that creates a 2d array (called matrix):

public static void main(String[] args) throws Exception {

    // will initialize the matrix with null references
    String[][] matrix = new String[4][3];

    // fill it with some values
    for (int i = 0; i < matrix.length; i++) 
        for (int j = 0; j < matrix[0].length; j++)
            matrix[i][j] = "Hello";

    // set a specific matrix element
    matrix[1][1] = "World";

    // print it
    for (int i = 0; i < matrix.length; i++) 
        System.out.println(Arrays.toString(matrix[i]));
}

It will print:

[Hello, Hello, Hello]
[Hello, World, Hello]
[Hello, Hello, Hello]
dacwe
  • 43,066
  • 12
  • 116
  • 140
1

Here is an example of instantiating and accessing members of a two dimensional String array.

public class TwoDimensionalArray {
    public static void main(String[] args) {
        String[][] array = new String[2][];
        array[0] = new String[2];
        array[0][0] = "apple";
        array[0][1] = "cherry";
        array[1] = new String[2];
        array[1][0] = "banana";
        array[1][1] = "grape";
        System.out.println(array[0][0]);
        System.out.println(array[0][1]);
        System.out.println(array[1][0]);
        System.out.println(array[1][1]);
    }
}

This program outputs

apple
cherry
banana
grape
Asaph
  • 159,146
  • 25
  • 197
  • 199
0

Not sure what you mean, but here is an example.

String[][] args = new String[10][10];
// Now you can iterate over arg and do many things.
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • can we use, public static void main(String [] [] args) – yasitha Feb 01 '11 at 08:02
  • @yasitha: You can create that method but it won't be called when your program starts. The method signature must be `public static void main(String[] args)` for it to be called when the class is executed. – Asaph Feb 01 '11 at 08:04
0

If you use this construction, how would you pass in the infromations? If it's 1D then the words are seperated by space, but how in 2D case? If you need to pass in 2d matrix you should pass it in from file. public static void main(String [] [] args)

Risto Novik
  • 8,199
  • 9
  • 50
  • 66