This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Multidimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multidimensional Arrays" Watch "Multidimensional Arrays" New topic
Author

Multidimensional Arrays

Aruna Balasuriya
Ranch Hand

Joined: Nov 14, 2009
Posts: 44
Why following code results a runtime exception

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Let's check out what each array contains. Initially:
a1 = {null}
a2 = {{null}, {null}}
a3 = {{{null, null, null}, {null, null, null}, {null, null, null}}, {{null, null, null}, {null, null, null}, {null, null, null}}, {{null, null, null}, {null, null, null}, {null, null, null}}}

a1 then becomes {A}
a2 becomes {{A}, {A}}
a3 becomes {{{A}, {A}}, {{A}, {A}}, {{A}, {A}}}

a3 is now no longer an A[3][3][3] but an A[3][2][1]. As such, there is no a3[2][2]. The highest indexes you can get is a3[2][1][0].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multidimensional Arrays
 
Similar Threads
Object reference array assignment
Array
help in array
Arrays
Dan Chisholm's Array Question