• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Multidimensional Arrays

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why following code results a runtime exception

 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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].
 
Uh oh, we're definitely being carded. Here, show him this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic