I was reading a tutorial about 2D arrays and I decided to construct something. I managed to construct something but when I compile I get "null" as my output. I cant seem to figure out the problem. Here is the sample code. I have also higlighted in red a line I dont understand because even if I seem to put the actual value i.e 10 it works well, so if someone can also explain that to me then I'd appreciate that. Thanx

Class A{
	public void in(String[][] arr)
	{
		for(int i=0;i<arr.length;i++)//cannot understand this part
		{	
			for (int j=0;j<arr[i].length;j++)//cannot understand this part
				arr[i][j]=".";
		}
	}
	
	public void out(String[][] arr)
	{
		for(int i=0;i<coordinates.length;i++)
		{
			
			for (int j=0;j<arr[i].length;j++)
				System.out.print(arr[i][j]);
				System.out.println();
		}
	}
}

Class B{
	public static void main (String[] args)
	{
		String [][] arr=new String[10][10];
               obj.out(arr);
        }
}

Post a compilable piece of code; what you posted isn't even remotely a valid Java program.

In Java, multi-dimensional arrays are represented as array of array. This allows you to ignore the second dimension when defining a two dimensional array. Generically speaking, this concept allows you to ignore all the dimensions except the first one. Also in case of a two dimensional array, the first slot of your array will hold a reference to another array and so on.

A simple example:

public class ArrayTest {

  public static void main(final String[] args) {
    arrayTest();
  }

  private static void arrayTest() {

    int[][] arrTwo = new int[3][];
    for(int i = arrTwo.length - 1; i >= 0; --i) {
      // arrTwo[i] is null since we ignored the second dimension
      // when declaring arrTwo
      System.out.println("arrTwo[" + i + "] -> " + arrTwo[i]);
    }
    System.out.println("\n\n");

    // When an array is defined, its slots hold the default value for the
    // type for which it is defined. Hence in case of an int array it
    // would be 0 while in the case of a String array it would be `null'.
    
    int[][] arrOne = new int[3][3];
    for(int i = arrOne.length - 1; i >= 0; --i) {
      // arrOne[i] is an array having length 3
      int[] tmp = arrOne[i];
      System.out.println("arrOne[" + i + "] -> " + tmp);
      for(int j = tmp.length - 1; j >= 0; --j) {
        System.out.println("arrOne[" + i + "][" + j + "] -> " + tmp[j]);
      }
    }
    System.out.println("\n\n");

    String[][] arrThree = new String[3][3];
    for(int i = arrThree.length - 1; i >= 0; --i) {
      // arrThree[i] is an array having length 3
      String[] tmp = arrThree[i];
      System.out.println("arrThree[" + i + "] -> " + tmp);
      for(int j = tmp.length - 1; j >= 0; --j) {
        System.out.println("arrThree[" + i + "][" + j + "] -> "
            + tmp[j]);
      }
    }
    System.out.println("\n\n");

    // You can ignore all but the first dimension because of the way Java
    // internally handles arrays; loop through the first dimension to
    // initialize the second dimension and do the same with second
    // dimension to initialize the third.
    String[][][] arrFour = new String[3][][];
    for(int i = 0, maxI = arrFour.length; i < maxI; ++i) {
      arrFour[i] = new String[3][];
      for(int j = 0, maxJ = arrFour[i].length; j < maxJ; ++j) {
        arrFour[i][j] = new String[3];
        for(int k = 0, maxK = arrFour[i][j].length; k < maxK; ++k) {
          arrFour[i][j][k] = i + "-" + j + "-" + k;
          System.out.println("arrFour[" + i + "][" + j + "]["
              + k + "] -> " + arrFour[i][j][k]);
        }
      }
    }
  }
  /*
                  [arr[0]]
                     |
                     |
                     |
                 [arr[0][0]]
                 /   |   \
                /    |    \
               /     |     \
              /      |      \
             /       |       \
            /        |        \
           /         |         \
 [arr[0][0][0]] [arr[0][0][1]] [arr[0][0][2]]
  */
}
Comments
good explanation
nice example

I couldnt post the whole code that is why i posted just part of it. Could you please explain to me the use of something like "arr.length" in the program?

Read the above post; if it's still unclear, it's time for you to go back to your class notes / tutorials.

This question has already been answered. Start a new discussion instead.