File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array" Watch "Array" New topic


srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225

prints null
prints nullnull
compile-time error
Run-time exception

answer:run-time exception

ArrayIndexOutOfBoundsException at line:1


SCJP 5.0<br /> <br />"Skills are started with learning and mastered with improvement. Nothing is hereditary except death" BUDDHA...
suresh mulagala
Ranch Hand

Joined: Feb 18, 2003
Posts: 41
Because a3 is being converted to a max of A[3][2][1] when you are doing

so the utmost you can do is a3[2][1][0]
swati cha
Ranch Hand

Joined: Jul 04, 2006
Posts: 40
if i compile the above program i am gettein error like..

A cannot be resolved to a type
could you explain this.
Srikanth Iyer
Ranch Hand

Joined: Apr 30, 2007
Posts: 52
in the above mentioned code your are tryiing to create and array of Type class A and thus you should declare a class named and thenn complie.

just add class a{} in your code and it will complie fine.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
In the first part:
A[] a1=new A[1];
A[][] a2=new A[2][1];
A[][][] a3=new A[3][3][3];

The arrays are created, and the default values (null) are put into their places.
Therefore the output is null.

Second part:

a1[0]=new A();

a1 looks like this:
a1: { the new A() }

an array with only one element, length of the array is one.

When you say

a2 looks like:
a2: { a1, a1 }
Now, a2 no longer has the length of three because it has been reassigned.

and finally

a3 looks like:
a3: { a2, a2, a2 }

But when you want to have:

Starting from left to right:
a3[2] points to a2 (all three elements of a3 are a2).

a3[2] [2] is the same as
but a2 has only a2[0] and a2[1] (see bold line above), therefore the exception.

Perhaps draw the arrays on a sheet of paper.


all events occur in real time
suresh mulagala
Ranch Hand

Joined: Feb 18, 2003
Posts: 41
Swathi try this...

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
This was a superbb explanation

by Mr Burkhard Hassel !!
srinivas sridaragaddi
Ranch Hand

Joined: Jul 24, 2007
Posts: 225
Thanks Burkhard for explaining so briefly....

Thank you for helping me understand many topics as you have replied to most of my posts...
It is sorta covered in the JavaRanch Style Guide.
subject: Array