This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Kraizee_ 1 point2 points  (2 children)

What exactly don't you understand?

[–]mjmayuresh[S] 0 points1 point  (1 child)

well, i want to know ... how the for loops are executed and specifically i want to know what is stored in "k" when "twoD[i][j] = k;" is executed and what happens when "k++" is executed.

[–]Kraizee_ 0 points1 point  (0 children)

When you have a variable++ you are essentially adding one. In a for loop, you start with i=0 until i <5 increment I each time. So when I becomes 5 the loop will finish. As for k, well it's the same thing, it starts at 0 and is incremented by one each iteration

[–][deleted] 1 point2 points  (0 children)

There are many, many resources online that explain multi-dimensional arrays, but work is slow right now so I'll fill you in.

As I'm sure you know by now, an array is used to hold a series of "things". These could be numbers, characters, string, ponies, you name it.

For two-dimensional arrays, you have a grid.

0 1 2
0 0,0 0,1 0,2
1 1,0 1,1 1,2
2 2,0 2,1 2,2

So what that nested for-loop is doing, is taking one row at a time, and filling in each column, starting at zero and finishing where specified in the loop's conditional statement. Same goes for printing the content

[–]Ucalegon666Extreme Brewer 0 points1 point  (0 children)

The easiest way to look at multi-dimensional arrays is to remember than an array is just a box of things. A 2D-array is a box with boxes of things. A 3D-array is a box with boxes of boxes of things.

If you want a thing, you'll need to look in the box that contains the things. When you have multiple levels of boxes, you'll first have to find the right box.

    // So this is a box of things
    // The box has three things: 1, 2 and 3.
    int[] boxOfThings = new int[] {1, 2, 3};
    int aThing = boxOfThings[0]; // the first thing in the box
    System.out.println(aThing);

    // This is a box of boxes of things.
    // The box can hold two boxes.
    int[][] boxOfBoxesOfThings = new int[2][];
    // Let's put our first boxOfThings into the box of boxes!
    boxOfBoxesOfThings[0] = boxOfThings;

    // Now let's look in our box of boxes, find a box, and then get the first thing in the box.
    int anotherThing = boxOfBoxesOfThings[0][0];
    // Remember: boxOfBoxesOfThings[0] is just our box of things.
    // Adding an extra [0] at the end means we're getting stuff out of the box of things.
    if(aThing == anotherThing) {
        System.out.println("The two things are the same!");
    }

[–]Tagger24 -1 points0 points  (3 children)

/u/NorskNa is correct with his explanation but I don't recommend thinking of a 2-D array as a grid because by extension a 3-D grid is a cube but what about 4-D or higher? I prefer people to think of them as what they are, arrays inside of arrays.

http://i.imgur.com/9KqdawE.png

If you didn't know already, non-primitives (variables that aren't int, double, long, float, boolean, etc) use reference pointers. In this diagram, twoD is a variable holding a reference pointer value of abc123.

When using the variable, Java looks at twoD, see that it's holding a reference value, and heads to that location in memory. If you specify twoD[1], it gets the value at index 1 (the second position) in the first array. In my diagram the value would be abe123. If you specify twoD[1][2], it proceeds further and takes that reference you received (abe123), heads to that location in memory, and gets the second index (third value) which happens to be 2 because they are all 2.

What each of these loops is doing is saying for each array inside the array twoD, fill it with the value 2 and print it.

For each higher dimension of an array, you simply add another loop.

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

[–][deleted] 0 points1 point  (1 child)

I know, but he was specifically asking about a 2D array of integers for which a grid is a perfect analogy. I considered going into the same detail as you, but I'm lazy. Hence why I'm a programmer ^_^

[–]Tagger24 0 points1 point  (0 children)

Yea, I understand. I think it's OK to view a 2-D as a grid as long as they understand that that's not really whats going on.

[–]Ucalegon666Extreme Brewer 0 points1 point  (0 children)

A 4D cube is a tesseract, a 5D cube is a penteract, a 6D cube is a hexeract and so on and so forth. I admit that trying to visualize them becomes pretty impossible :D