Marcus Laubli

Ranch Hand

Posts: 116

posted 12 years ago

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 ]

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)

Anand Ko

Ranch Hand

Posts: 79

posted 12 years ago

Always try to picture it. I mean draw it on the paper.

int [] [] [] x = new int [3] [] [];

// This is 3-D dimension

int i, j;

x[0] = new int [4] [];

//First element of 3-D pointing to 2-D with 4 rows

x[1] = new int [2] [];

//Second element of 3-D pointing to 2-D with 2 rows

x[2] = new int [5] [];

//Third element of 3-D pointing to 2-D with 5 rows

x[0] -> - - - -

- - - -

- - - -

- - - -

x[1] -> - - - -

- - - -

x[2] -> - - - -

- - - -

- - - -

- - - -

- - - -

Here i have just assumed some columns length i.e 4 for my understanding.

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

for (j = 0 ; j < x[i].length ; j++) { // loops for i=0;3

x [i][j] = new int [i + j + 1]; // x[0-2][0-2]

System.out.println("size = " + x[i][j].length);

}

}

So when x[2][j] = it will throw runtime exception

int [] [] [] x = new int [3] [] [];

// This is 3-D dimension

int i, j;

x[0] = new int [4] [];

//First element of 3-D pointing to 2-D with 4 rows

x[1] = new int [2] [];

//Second element of 3-D pointing to 2-D with 2 rows

x[2] = new int [5] [];

//Third element of 3-D pointing to 2-D with 5 rows

x[0] -> - - - -

- - - -

- - - -

- - - -

x[1] -> - - - -

- - - -

x[2] -> - - - -

- - - -

- - - -

- - - -

- - - -

Here i have just assumed some columns length i.e 4 for my understanding.

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

for (j = 0 ; j < x[i].length ; j++) { // loops for i=0;3

x [i][j] = new int [i + j + 1]; // x[0-2][0-2]

System.out.println("size = " + x[i][j].length);

}

}

So when x[2][j] = it will throw runtime exception

Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)

posted 12 years ago

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;

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].

ArrayIndexOutOfBoundsException.

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?

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].

ArrayIndexOutOfBoundsException.

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 Bates

author

Sheriff

Sheriff

Posts: 8945

17

posted 12 years ago

Hey Marcus -

We feel your pain, we really do... we had to study for this sucker too

But, we do want to make sure that you're really prepared, and there will be some real time wasters on the real exam.

- Bert

We feel your pain, we really do... we had to study for this sucker too

But, we do want to make sure that you're really prepared, and there will be some real time wasters on the real exam.

- Bert

Spot false dilemmas now, ask me how!

(If you're not on the edge, you're taking up too much room.)

Mike Gershman

Ranch Hand

Posts: 1272

Marcus Laubli

Ranch Hand

Posts: 116

posted 12 years ago

Thanks for the encouragement! At this point, I need all of it I can get.

I guess that at this point I'll hvae to rely on the "method" of solving something like this, rather than on the complexity at issue.

Marc, I really do appreciate the warning on the ArrayIndexOutOfBoundsException. I took it to heart.

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.

Thanks again guys. I'll be back!

Marcus L�ubli

I guess that at this point I'll hvae to rely on the "method" of solving something like this, rather than on the complexity at issue.

Marc, I really do appreciate the warning on the ArrayIndexOutOfBoundsException. I took it to heart.

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.

Thanks again guys. I'll be back!

Marcus L�ubli

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

posted 12 years ago

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

i=0, j=0, output=1

i=0, j=1, output=2

i=0, j=2, output=3

i=0, j=3, output=4

i=1, j=0, output=2

i=1, j=1, output=3

i=2, j=0, output=3

i=2, j=1, output=4

i=2, j=2, output=5

i=2, j=3, output=6

i=2, j=4, output=7

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...

i=0, j=0, output=1

i=0, j=1, output=2

i=0, j=2, output=3

i=0, j=3, output=4

i=1, j=0, output=2

i=1, j=1, output=3

i=2, j=0, output=3

i=2, j=1, output=4

i=2, j=2, output=5

i=2, j=3, output=6

i=2, j=4, output=7

*~Joe Strummer*

sscce.org

posted 12 years ago

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...

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...

size = 3

size = 4

size = 5

size = 6

size = 7

*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...

size = 3

size = 4

size = 5

size = 6

size = 7

*~Joe Strummer*

sscce.org