File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes error using 3d array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error using 3d array" Watch "error using 3d array" New topic
Author

error using 3d array

Anshul Singhal
Greenhorn

Joined: Dec 14, 2009
Posts: 18


the code compiles but at the run time it halts and shows

Exception in thread "main" java.lang.ArrayIndexOutOfBoundException at 2 at anshul.main(practice.java)

Can some one please explain what does it mean??
Thankyou
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
It means that you've exceeded the size of your array somewhere, probably in that for loop, but I'm too lazy to count all of your initializations to figure out which dimension was violated. Your loop expects 15 i elements, 22 j, and 2 k for the array[i][j][k]. So you should ensure that all of your initializations include at least 15 i elements (0 - 14), 22 j (0 - 21), and 2 k (0 - 1). That would be 15 x 22 x 2 = 660 elements. Phew!

I see your first initialization has 22 elements. Shouldn't that be 15? Did you swap the i and j dimensions? It's hard to visualize and work with a 3(or more)-dimensional array, so I appreciate the challenge you have to keep it all straight.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Anshul Singhal
Greenhorn

Joined: Dec 14, 2009
Posts: 18
Greg, Thanks for replying

well, the array was of the dimension on 15*22*2. Thus in the initialization part it has 15 rows and 22 columns. I tried swapping the i and j dimensions but the program is still showing the same error.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11448
    
  16

if you format your array a little better:

you can see what you have is an array of 15 elements (the i dimension).

Each of those elements are arrays of TWO elements (the j dimention), each of which contain arrays of 22 (k)elements. Your for loops should be like this:




There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Sorry, thought about it some more, but I'm glad my question (however wrong) got you to think about it and try something.

A one-dimensional array, array1D[5], has 5 elements that we visualize in a row, or as a single row of 5 columns:


A two-dimensional array, array2D[4][5], we visualize in rows and columns. In this case, 4 rows of 5 columns:


Notice that the last dimension remains the number of columns.

A three-dimensional array, array3D[2][4][5] can be thought of as a 4 x 5 array 2 deep or two 4 x 5 arrays:


Notice that the last dimension describes the number of columns.

So, what is your array? Since the one line I counted had 22 members, there must be 22 columns, so that would be the last dimension. I'm not sure how the rest of it is organized.

I haven't given you a complete answer to your question, but I've certainly given you enough to complete the answer yourself.

Is 4D next?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
As Fred has hinted, there is no such thing as a 2D array, let alone 3D and 4D. There are only arrays or arrays.

You should always use i < myArray.length in the for loop when you are iterating an array. It would be better still to use a for-each loop.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11448
    
  16

also, using the array.length would allow you to have a ragged-array.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error using 3d array