This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes an array and two loops walk into a bar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "an array and two loops walk into a bar" Watch "an array and two loops walk into a bar" New topic
Author

an array and two loops walk into a bar

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
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?


giddee up
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Jasper Vader:
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Paverd
Ranch Hand

Joined: Nov 17, 2002
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!


SCJP 1.4
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
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

Joined: Nov 17, 2002
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

Joined: Jan 24, 2003
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).

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
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

Joined: Nov 17, 2002
Posts: 115
Originally posted by Jasper Vader:
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
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
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

Joined: Aug 26, 2001
Posts: 7610

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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
John Paverd
Ranch Hand

Joined: Nov 17, 2002
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

Joined: Aug 26, 2001
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: an array and two loops walk into a bar