This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.

I need some help knowing how to unravel one of these ugly multidimensional array problems. Please explain it line by line. I want to be able to use this method in the exam.

I took the following example out of K & B's Study Guide, page 48 & 49. It's question number 11:

Then we have the following answers:

A. 7 B. 9 C. 11 D. 13 E. Compilation fails F: An exception is thrown at runtime

I'm not arguing that I got answer F. I let it through the compiler before I posted. My question is how I can do several of these in 2 hours!

Now, I admit that I suffered encephalitis 3 1/2 years ago, then found out that I lost 40% of my cognitive and memory functionality from it, AND I'm just studying to take 310-035, but doesn't this border on abuse?

I really need a methodical way to take this kind of monster apart within a couple of minutes.

Thank you so much for any help you can give!

Marcus L�ubli

P.S. problem 12 on page 49 is no better! HELP!!

( tags added) [ January 06, 2005: Message edited by: Barry Gaunt ]

Marcus L�ubli, SCJP 1.4, CLP 5.0, SCWCD 1.4 (preparing)

All of that jagged multi-dimensional array stuff is just there to distract you. The problem is in the boolean test of the outer loop:

for(i = 0; i < x[i].length; i++){...

When i = 0, x[i].length is x[0].length, which is 4. When i = 1, x[i].length is x[1].length, which is 2. When i = 2, x[i].length is x[2].length, which is 5. When i = 3, x[i].length is x[3].length, which is... Whoops! There is no x[3].

So here's the tip: Before investing time in the computational details of a messy problem, make sure the structure is valid. And whenever you see a variable as an array index, that should raise a red flag to check the bounds.

I doubt you'll see anything this messy on the actual exam, but working through these (without stressing too much about the time factor) is excellent practice. Why? Because the next time you see something like this, you'll be sure to check the array bounds, right?

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org

Bert, I can only thank you for taking the time to write a book like this one. It's grammer is good, wording is simple, and English is pleasant. I speak 3 languages and program(ed) half a dozen (before my illness). We were so far behind in our JB-341 class here in Mesa, AZ that my prof went out and bought us a gift, your Study Guide! Wow, what a winner!

I never thought that learning a programming language would be this difficult, however, I've found good tools which I will use to get me through this.

Originally posted by Mike Gershman: Line 8 is actually: for (i = 0 ; i < x.length ; i++) {...

Ah... Well, x.length is set at 3. So in the outer loop, i just goes from 0 through 2.

Then in the inner loop...

When i=0, j goes from 0 through 3. When i=1, j goes from 0 through 1. When i=2, j goes from 0 through 4.

So we have a total of 11 iterations of the inner loop. And with each of these iterations, we create a new array, x[i][j]; then print out the new array's length, which is i+j+1. So...

When the dimension gets above 2 or 3, it's difficult to "envision" a multidimensional array as a whole. But remember, a multidimensional array is just an array of arrays of arrays... So take it one array at a time.

I tend to envision an array as a filing cabinet. In a multidimensional array, each drawer simply contains another filing cabinet.

Inside the first drawer of filing cabinet x, we have a filing cabinet with 4 drawers. Inside the second drawer of filing cabinet x, we have a filing cabinet with 2 drawers. And inside the third drawer of filing cabinet x, we have a filing cabinet with 5 drawers.

Now, the outer loop of this program simply opens each of the 3 drawers in filing cabinet x. And whenever one of these drawers is opened, the inner loop takes over to open each drawer of the inner filing cabinet.

While we're in each of these drawers, we add another filing cabinet (the third dimension). The number of drawers in each of these new filing cabinets depends on which drawer of x it's in (0, 1, or 2) and which drawer of the inner filing cabinet it's in. Each time, we output the size of the new cabinet.

So if the original question was how many lines of output we'll have, then the answer is 4 + 2 + 5 = 11. If it's asking what the last output size will be, we can jump ahead to the last iteration: The third drawer of filing cabinet x contains a cabinet with 5 drawers. So the cabinet we put in x[2][4] will have 2+4+1=7 drawers.

For the sake of completion, here's the entire looping process...

We open the first drawer of filing cabinet x. Inside is another filing cabinet with 4 drawers. In x[0][0], we add a new filing cabinet with 0+0+1=1 drawer. In x[0][1], we add a new filing cabinet with 0+1+1=2 drawers. In x[0][2], we add a new filing cabinet with 0+2+1=3 drawers. And in x[0][3], we add a new filing cabinet with 0+3+1=4 drawers. So...

size = 1 size = 2 size = 3 size = 4

Next, we open the second drawer of filing cabinet x, and repeat the process. The filing cabinet in x[1] has only two drawers. In x[1][0], we add a new filing cabinet with 1+0+1=2 drawers. And in x[1][1], we add a new filing cabinet with 1+1+1=3 drawers. So...

size = 2 size = 3

Finally, we open the third drawer of filing cabinet x, and repeat the process. In x[2][0], we add a new filing cabinet with 2+0+1=3 drawers. In x[2][1], we have 2+1+1=4 drawers. In x[2][2], we have 2+2+1=5 drawers. In x[2][3], we have 2+3+1=6 drawers. In x[2][4], we have 2+4+1=7 drawers. So...