Before we can work with a two dimensional String array, we need to declare one first. For this, we can use the square brackets. We need to place two pairs of square brackets and we can do it in two ways. The first is to place it after the String keyword, for example:
String[][] myTwoDimensionalStringArray;
Another way is to place the two pair of square brackets after the variable name, like below. Both have the same effect of declaring two dimensional String Array.
String myTwoDimensionalStringArray[][];
String myTwoDimensionalStringArray[][] = new String[8][8];But it is allowed if we just specify the size of the first dimension and skip the second. Here is an example:
String myTwoDimensionalStringArray[][] = new String[8][];
But the compiler will raise an error if we don't specify both size, for example:
String myTwoDimensionalStringArray[][] = new String[][];
Note that if we only specify the size of the first dimension, it means we can use different sizes on the second dimension for each item on the first. Here is an example code to illustrate:
String myTwoDimensionalStringArray[][] = new String[4][]; myTwoDimensionalStringArray[0] = new String[2]; myTwoDimensionalStringArray[1] = new String[4]; myTwoDimensionalStringArray[2] = new String[5]; myTwoDimensionalStringArray[3] = new String[3];For the code sample above, we are declaring an array of String array. Specifically, we have 4 String arrays, and each have different length. The first String array has the size of two, the second the size of four, and so on. This makes two dimensional array flexible because we are not forced to have fixed size on the second dimension.
String myTwoDimensionalStringArray[][] = {{"Apple", "Banana"}, {"Pork", "Beef"} };We are declaring a two by two String array for the code above. But as discussed earlier, the length of the second dimension does not need to be the same for all items. For example:
String myTwoDimensionalStringArray[][] = {{"Apple", "Banana"}, {"Pork", "Beef", "Chicken"}, {"Carrots"} };In the example above, the first array has length of two, the second has length of three, and the last one only has a single element. We can also declare a two dimensional String Array with blank or 0 length like this:
String myTwoDimensionalStringArray[][] = {}; System.out.println( myTwoDimensionalStringArray.length );The output of the code will be zero. Here is another way of specifying values for our two dimensional String array:
String myTwoDimensionalStringArray[][] = new String[2][]; myTwoDimensionalStringArray[0] = new String[] {"Apple", "Banana"}; myTwoDimensionalStringArray[1] = new String[] {"Pork", "Beef"};And here is another way of writing the code above resulting in the same content:
String myTwoDimensionalStringArray[][] = new String[2][2]; myTwoDimensionalStringArray[0][0] = "Apple"; myTwoDimensionalStringArray[0][1] = "Banana"; myTwoDimensionalStringArray[1][0] = "Pork"; myTwoDimensionalStringArray[1][1] = "Beef";
Going through all the contents of a two dimensional String Array is simple. We just need to loop through each dimension and visit the contents. Here is a simple example of that:
String myTwoDimensionalStringArray[][] = {{"Apple", "Banana"}, {"Pork", "Beef", "Chicken"}, {"Carrots"} }; for (int x = 0; x < myTwoDimensionalStringArray.length; x ++) { String subArray[] = myTwoDimensionalStringArray[x]; System.out.println( "Length of array " + x + " is " + subArray.length ); for (int y = 0; y < subArray.length; y ++) { String item = subArray[y]; System.out.println( " Item " +yx + " is " + item ); } }The output of the code above will be:
Length of array 0 is 2 Item 0 is Apple Item 1 is Banana Length of array 1 is 3 Item 0 is Pork Item 1 is Beef Item 2 is Chicken Length of array 2 is 1 Item 0 is Carrots