Need some explanation in 2D String arrays

In a nested for loop for underlying array of 2D String, I came across this:

  array = new String [5][10]; for(int i=0; i<array.length;i++) { for(int j=0; j<array[0].length;j++) { System.out.print(array[i][j]="*"); } System.out.println(""); } 

Now this is what I want to know why the second for statement includes array[0].length rather than array.length , as in the for clause before it?

All that I could extract from this, while the experiment was, if both for statements contained array.length and the 2D array was 5x10, it would print it as 5x5, but with array[0].length it would print correct 5x10.

So why does this small adjustment fix everything?

+4
source share
4 answers

You are dealing with a 2D array. array.length essentially gives you the number of rows, but array[0].length gives the number of columns (at least for non-jagged arrays). Take this example:

 String[][] array = new String[][]{ {"1","2"}, {"3","4"}, {"5","6"} }; 

Here array.length is 3 (the entire 2D array consists of three 1D arrays), but array[0].length is 2 (the length of each composite array). Your first for -loop loop over the entire array of arrays, but your second loop over every compound array that the outer loop encounters. Thus, the inner loop should only be loops to (but not including) array[0].length .

+4
source

Aa 2D array is similar to the matrix represented by an array of arrays of String objects. When you define:

 array = new String [5][10]; 

You say, "I want an array of 5 string arrays of length 10," something like this:

  [String[10]][String[10]][String[10]][String[10]][String[10]] 

Here:

 for(int i=0; i<array.length;i++) { for(int j=0; j<array[0].length;j++) //I'd recommend j < array[i] here. Nothing //changes on this case, but it would bring //some trouble if the second dimention of the //array was not the same for every case. { System.out.print(array[i][j]="*"); } System.out.println(""); } 

The first is to iterate through an array of five String arrays, so I go from 0 to 4 , and the length of the array is 5. The second is to iterate through the indices of String[] objects contained in the first array (one with a length of 5 ). These arrays have a length of 10 elements.

+4
source

This is because array[0] is actually an array of String[] .

0
source

The second for statement is required to get the number of columns in the array, 10 in this case.

  • array.length - no. lines
  • array[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)); 
0
source

Source: https://habr.com/ru/post/1445897/


All Articles