简体   繁体   中英

Need some explanation on 2D String arrays

In the nested for loop for a basic 2D String array 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 does the second for statement include array[0].length rather than array.length like in the for statement before it?

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

So why does this little adjustment fix everything?

You're 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 is composed of three 1D-arrays), but array[0].length is 2 (the length of each constituent array ). Your first for -loop loops over the whole array-of-arrays, but your second loops over each constituent array that is encountered by the outer loop. Therefore, the inner loop should only loop up to (but not including) array[0].length .

Aa 2D array is like a matrix, represented by an array of arrays of String objects. When you define:

array = new String [5][10];

You are saying "I want an array of 5 string arrays with a length of 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 for iterates over the array of five String arrays, that's why i goes from 0 to 4 and the array's length is 5. The second for iterates over the indexes of the String[] objects contained on the first array (the one with length 5 ). This arrays have a length of 10 elements.

这是因为array[0]实际上是一个String[]数组。

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

  • array.length - no. of rows
  • array[0].length - no of columns

This array is assumed square rather than jagged so using array[0] is deemed safe to use in this example.

Let's 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 are only displaying 5 x 5 entries, missing half the number of entries. Also if the array had originally been 10 rows x 5 colums, an ArrayIndexOutOfBoundsException would have been throws as the number of columns would have been exceeded when j = 5 .

A convenient way of displaying 2D arrays is:

System.out.println(Arrays.deepToString(array));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM