adiprovista
adiprovista

Reputation: 39

Populating a two-dimensional arrays in Java

So this question is more theoretical since I am just starting to learn the Java language. It might sound very basic but I wanted to ask it anyway.

This is the code of my two-dimensional array.

int matrix[][] = new int[5][];
for (int i = 0; i < matrix.length; i++){
        matrix[i] = new int[i];


            for(int j = 0; j < matrix[i].length; j++){
                matrix[i][j] = i;
            }
        }

The first loop sets the number of elements of each inner array.

When I assign "i" for the matrix[ i ][ j ], the result would be:

matrix[0] is <>
matrix[1] is <1>
matrix[2] is <2, 2>
matrix[3] is <3, 3, 3>
matrix[4] is <4, 4, 4, 4> 

And the result for "j" in the same code:

for(int j = 0; j < matrix[i].length; j++){
                matrix[i][j] = j;
            }

would be:

matrix[0] is <>
matrix[1] is <0>
matrix[2] is <0, 1>
matrix[3] is <0, 1, 2>
matrix[4] is <0, 1, 2, 3>

So I try to understand as to why when I use the "i" for populating the arrays I have repeating numbers in the arrays, while I use the value of "j" it yields values starting from 0, 1, 2...

So would you please explain it to me step by step how for loops are iterating the arrays, the concept of iterating the arrays? I would really appreciate the given help! Thank you!

Upvotes: 0

Views: 3941

Answers (3)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

You have nested loops; the first value will repeat for every iteration of the second loop (for the first example). So what we have, given a simple example, is...

On first iteration of outer loop - i = 1, the inner loop j= 1.

So we have 1[1, 2, 3, ..., n].

The second example j "walks" one-for-one (i.e.; 1, 2, 3, 4...). j represents the

"[ 1, 2, 3,..., n]" of the above example.

That is the nature of nested loops.

Upvotes: 1

Timothy Groote
Timothy Groote

Reputation: 8653

Have a commented version:

Tip : writing comments like these yourself when you don't understand how a particular piece of code works can help figure it out.

//for every "available space" in our "matrix" variable
//we take one "step".
//this "step" is stored in a variable named "i"
for (int i = 0; i < matrix.length; i++)           //<-- outer loop starts here
{
        //initialise an array
        //this irray is as long as the number of loops we've made. (as long as "i" is high)
        //so the first one will be 0 length, the first will be 1 length, etc.
        matrix[i] = new int[i];


        //now, we start on the inner loop. keep in mind that we left "i" the same value.
        //for every available space in the array we just made, we take one "step"
        //this "step" is stored in a variable named "J"

        for(int j = 0; j < matrix[i].length; j++) // <-- inner loop starts here
        {
            //since we don't touch "i", it will be the same for as long as this loop runs.
            matrix[i][j] = i;

            //since we update "j" in every step of this loop, it will be 1 higher every time this loop runs.
            matrix[i][j] = j;
        } // <-- inner loop ends here

 } // <-- outer loop ends here

Upvotes: 1

Federico Ponzi
Federico Ponzi

Reputation: 2785

Let's pick a simple array, called i:

i = [a, b, c]

Now let's create another array for a,b,c:

a=[ 0, 1, 2 ] 
b=[ 0, 1, 2 ]
c=[ 0, 1, 2 ]

Basically, we have this:

row a = 0 1 2
row b = 0 1 2
row c = 0 1 2

The first for (the i), loop on the rows (a, b, c). The second for, loops on the column (0, 1, 2)

So if you say "row[a][2]" You have to pick the a row, and the 2 (third in java) column.

Upvotes: 0

Related Questions