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?

2
  • 1
    The crucial issue is that a 2D array is an array of arrays. Commented Nov 14, 2012 at 1:12
  • I'm not sure if you're trying to troll.. @__@, I'm well aware of that though. Commented Nov 14, 2012 at 1:13

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

7 Comments

If I changed it to array[i].length instead of array[0].length what would happen?
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.
Same as in? And by jagged I assume you mean: 136 258 479
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.
What if it contained array.length instead, what would be the difference.
|
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.

2 Comments

So what if I changed the second for to array.length what would the problem be?
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
0

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

Comments

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));

2 Comments

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?
By using array.length in the second for loop, only half the String values would be assigned & the rest would remain as null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.