The second for statement is required to get the number of columns in the array, 10 in this case.
array.length - no. linesarray[0].length - no columns
This array is considered square, not jagged, so using array[0] is considered safe for use in this example.
Look at the wrong way: By doing this:
for (int i=0; i<array.length;i++) { for(int j=0; j < array.length;j++) { System.out.print(array[i][j]="*"); } }
you only show 5 x 5 entries, half the number of entries is missing. In addition, if the array initially had 10 rows x 5 columns, an ArrayIndexOutOfBoundsException would be thrown because the number of columns would be exceeded if j = 5 .
A convenient way to display 2D arrays:
System.out.println(Arrays.deepToString(array));
source share