# an array and two loops walk into a bar

Ranch Hand
Posts: 284
class A {
public static void main (String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a1[j][i]);
}
}
}
}

Q: What is the result of attempting to compile and run the above program?

A: If array index i had been the first index of the array, a1, then the output would have been 123456789. Since i is the second index of the array the output is 147258369.
the question is one of DanChilsolm's.
I am just a little uncertain about how it works, with the loops and all. i guess it is straight forward?

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
class A {
public static void main (String[] args) {
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a1[j][i]);
}
}
}
}

Java doesn't really do multi-dimensional arrays. Instead it does arrays containing arrays. So there are three arrays {1,2,3}, {4,5,6}, and {7,8,9}. So
a1[1] = {1,2,3}
a1[2] = {4,5,6}
a1[3] = {7,8,9}
Therefore a1[2][3] = 6.
The loop says print:
ai[1][1] = first entry in the first array = 1
a1[2][1] = first entry in the second array = 4
a1[3][1] = first entry in the third array = 7
etc.

John Paverd
Ranch Hand
Posts: 115
A 2 dimensional (non-ragged) array can be thought of as a matrix - the first dimension represents a row, and the second dimension represents a column within the row. Each set of inner braces in the array intializer specifies 1 row of the matrix. You can visualize the matrix looking like this:
123
456
789
The code goes down the columns of the matrix instead of across the rows, giving the output 147258369
And don't leave us hanging - give us the punchline to the joke!

Ranch Hand
Posts: 284
Hmmm i am editing out what i have posted cos i was confused and the logic was not there.
SO far, all that i am understanding is that a symmetrical multi dimensional array can be seen like a matrix.
i like that idea.
123
456
789
and how does the counter go down the columns?

ps - thought i might generously leave the punchline open for anyone who could think of one
[ January 28, 2003: Message edited by: Jasper Vader ]

John Paverd
Ranch Hand
Posts: 115
Jasper
First, Thomas Paul's answer is the more technically accurate one - I just find it helpful to think of "rectangular" arrays of arrays as matrices.
To help yourself understand what is going on, replace the System.out.print with:
System.out.println("a[" + j + "][" + i + "]=" + a1[j][i]);
Because j is the inner loop index, it changes much faster than i, the outer loop index. j is an index into the the first array dimension, or row, while i is an index into the 2nd dimension, or column of the matrix. Therefore the first iteration of the outer loop will print the contents of a[0][0], then a[1][0], then a[2][0], i.e. column 0 in each of the successive rows.
And when you think of a punchline, I will post a less confusing answer

Dan Culache
Ranch Hand
Posts: 70
I guess it's somehow like this:
If you have a "matrix" declared like this:
int[][] a1 = {{1,2,3},{4,5,6},{7,8,9}};
that you represented like
123
456
789
then the first index of a1[i][j] (here i) is the index of the row and the second (j) is the index of the column.
But if you want to "go down the columns" first you have to increment the row index (the i) in the inner loop (or first) and the column index (the j) in the outer loop (second).

Ranch Hand
Posts: 284
PUNCHLINE
an array and two loops walk into a bar...and are promptly thrown out due to the barman mistaking their recursive calls as abusive

.....best i could do i'm afraid...was thinking of making a pun on the pun joke where the piece of string walks into the bar after frizzing up one end and then, when the barman says "we dont serve string here and you're a piece of string", replying "I'm afraid not" (a frayed knot)...but that is an old joke which only raises a half hearted chuckle on the best of occasions...

John Paverd
Ranch Hand
Posts: 115
PUNCHLINE
an array and two loops walk into a bar...and are promptly thrown out due to the barman mistaking their recursive calls as abusive

The barman, who was a virtual machine at serving drinks, took exception to their recursive calls, and threw them out of the class Bar?
Sorry, I couldn't resist

Ranch Hand
Posts: 284
lol, if i had quick access to a url of a madly applauding emoticon, you know i would be using that right now!

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610

[ January 30, 2003: Message edited by: Valentin Crettaz ]

John Paverd
Ranch Hand
Posts: 115
Valentin

So are those icons going to be added to the graemlins on the "post a reply" page?

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
I'm not sure, but if you need them you can just use them, here are their address:
http://www.valoxo.ch/thumb.gif
http://www.valoxo.ch/thumbup.gif
http://www.valoxo.ch/bounce.gif
just put the link between img UBB tags