4

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?

SelfDeceit
  • 107
  • 1
  • 2
  • 13

4 Answers4

5

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.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • If I changed it to `array[i].length` instead of `array[0].length` what would happen? – SelfDeceit Nov 14 '12 at 01:05
  • Nothing in this case - because it is a *square* array, so `array[i].length` will always be the same. If you had a jagged array you would have to do that. – arshajii Nov 14 '12 at 01:05
  • Same as in? And by jagged I assume you mean: 136 258 479 – SelfDeceit Nov 14 '12 at 01:09
  • Same as in `array[i].length` will always be the same number for each `i` (assuming `i` is in range). In your case it will always be **`10`**. Jagged arrays are explained [here](http://stackoverflow.com/questions/10271540/java-jagged-array). – arshajii Nov 14 '12 at 01:11
  • What if it contained `array.length` instead, what would be the difference. – SelfDeceit Nov 14 '12 at 01:23
  • Well then we would have a problem, because `array.length` is the total number of arrays that your 2D-array contains, but this doesn't have to be (and isn't in this case) equal to *length of these individual arrays* (`array[0].length`), so you will likely get an `ArrayIndexOutOfBoundsException` or not loop over the entire array (in this case the latter). – arshajii Nov 14 '12 at 01:25
  • Ah so it should be recognized as `array[i].length` to make sure the column is verified. – SelfDeceit Nov 14 '12 at 01:30
4

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.

Fritz
  • 9,987
  • 4
  • 30
  • 49
  • So what if I changed the second `for` to `array.length` what would the problem be? – SelfDeceit Nov 14 '12 at 01:16
  • Since there are 5 rows and 10 columns in this 2D array. Therefore, in the first loop each element you get is a row with 10 strings. So, you want the second loop to run 10 times. Hence you do array[0].length which evaluates to 10 and not array.length which evaluates to 5 – Robin Chander Nov 14 '12 at 02:44
0

It's because array[0] is actually a String[] array.

ShyJ
  • 4,560
  • 1
  • 19
  • 19
0

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));
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • So if both `for` statements contained `array.length` what would be the issue? Would it just create a "square-type" table by just taking the number of rows as the number of columns? – SelfDeceit Nov 14 '12 at 01:27
  • By using `array.length` in the second `for` loop, only half the `String` values would be assigned & the rest would remain as `null`. – Reimeus Nov 14 '12 at 02:53