Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Mock Exam Array question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this question on Dan Chrisholm's mock exam:
http://www.danchisholm.net/apr18/topic/section1/arrays1.html
Questions 17

What is the result of attempting to compile and run the above program?
I didn't understand why it gives a runtime-exception, and the answer posted, can anyone put it in simpler terms please
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, let me try to make this easy to understand - I'll do my best.
a3 references a 3 dimensional array. Another way to look at a a 3-D array is as a single dimensional array in which each element of the array references a 2-D array. Think of it like this (forgive the poor ASCII art):

However, in this question, in line 7, you're assigning each of the elements of a3 to a reference to a 2-D array which has dimensions 2x1. Therefore, your picture looks something like this now:

Notice that now each element of a3 references another array which has dimensions 2x1. I've also written in the index values for each of the elements indexed by a3[2]. As you can see, the array element a3[2][2][2] no longer exists! That's why you end up with the run-time exception.
I hope that helps,
Corey
 
David Attard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Thanks a lot!
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic