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
• Paul Clapham
• Ron McLeod
• Tim Cooke
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Devaka Cooray
• Jeanne Boyarsky
Saloon Keepers:
• Jesse Silverman
• Stephan van Hulst
• Tim Moores
• Carey Brown
• Tim Holloway
Bartenders:
• Jj Roberts
• Al Hobbs
• Piet Souris

# Array Problem

Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:
From Dan Chisholm

I thought it will be a runtime exception.
Prints null
and runtime exception

Can anybody explains why is it so?
[ December 15, 2006: Message edited by: Sanjeev Kumar Singh ]

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
in the statement a3[0]=a3[1]=a3[2]=a2; you are assigning a2 to all the indexes of a3. However a2 have been declared with only as

A[][] a2=new A[2][1]

in which we can refer only as

a3[0 to 2 ][0][0]
and
a3[0 to 2 ][1][0]
code:

hope that will help otherwise it will give array index out of bounds exception

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
in the statement a3[0]=a3[1]=a3[2]=a2; you are assigning a2 to all the indexes of a3. However a2 have been declared with only as

A[][] a2=new A[2][1]

in which we can refer only as

a3[0 to 2 ][0][0]
and
a3[0 to 2 ][1][0]
code:

hope that will help otherwise it will give array index out of bounds exception

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
And this exception will not arise when are trying to print the same in the first print statement.

So it prints null and then gives an exception

Sanjeev Singh
Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:

So it prints null and then gives an exception.

Unfortunately what I gets is .

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at A14.main(A14.java:11)
null
Java Result: 1

It means an exception and then null.
[ December 15, 2006: Message edited by: Sanjeev Kumar Singh ]

Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Sanjeev,

Can you explain it, if you understood??

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Hi Sanjeev,

I tried this, and I understood it like, you are assigning in Line 7 a3[0] = a3[1] = a3[2] = a2; which is as good as saying, = new Main[2][1]; so it gives the exception. Try changing the line 8 as ystem.out.print(a3[2][2]);, still it gives you the exception. But when you say System.out.print(a3[2][1]);, it prints null. Hope you got the clue.

Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
initially

So in the first case it prints null for a3[2][2][2]

where as after the re assignment

what you have is

So now when you try to access the a3[2][2][2], sure their is an exception
Here there is no a3[0-1-2][2]
Try to follow the internal representation of the arrays in heap, then its quiet easy.

Sanjeev Singh
Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:
As per as the thigs which I understands
The first SOP should print null and
The second SOP should prints ArrayIndexOut..Exception
but I gets the reverse order.Is this happening with you guys also?

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Sanjeev, it is not the reverse order. Comment the last line and still you will get null for // 4. What we have to know is that whenever there is a run time exception, it gets printed out first followed by any println statements in the order they are written.

S Thiyanesh
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
Jothi,
I think you misunderstood a bit.
After runtime exception only the cause of the exception is printed and the program terminates if there is no handler.

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Thiyanesh, Can you explain it very broadly?? I got confused.

S Thiyanesh
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
Keep it simple man.
Just after the exception there is nothing going to get printed except the cause of the exception(trace);
In our discussion when first print statement is executed, there is null stored at the index a3[2][2][2] and is printed
But after the assignment, we don't have a a3[2][2][2]
So in the second print statement its an array index out of bounds exception.
Then the program halts and none of the statement is executed until unless there is try{}catch{} module

Hope you understood. Just try a few exception creating statements and look at the flow. You will make it easy with practise.

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Thiyanesh, thanks for the explanation. Can you also please explain once again the assignment for the arrays. I'm only partially understanding your explanation??

S Thiyanesh
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
Jothi,
Let me know if i am going outside,
Think of pointers in "C"

So for a3[][][],
We have each element of a3[3rd Dim] holds a reference to another two dimensional array.
Further each of a3[2nd dim] holds a reference to another one dimensional array.
Then each of these final one dimensional arrays have either primitives for if its a primitive array or references for the objects if an object array.

The point is the real values of primitives or the references to our objects are placed as the one dimensional array.

Can you get this.

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Can you just give a clear picture of what the line below means,

a2[0] = a2[1] = a1;

I'm not familiar with pointers...

Sanjeev Singh
Ranch Hand
Posts: 381
• Number of slices to send:
Optional 'thank-you' note:
Jothi,It means that a2 which is a 2D array

A13[][] a2 = new A13[2][1];

and has a size enough to refer to 2 1D array is refering to same 1D array named a1.

a2[0] = a2[1] = a1;

[ December 15, 2006: Message edited by: Sanjeev Kumar Singh ]

S Thiyanesh
Ranch Hand
Posts: 142
• Number of slices to send:
Optional 'thank-you' note:
a2[][] is a two dimensional array.
a1[] is a single dimensional array.
Each 2nd dimension in a two dimensional array should have a reference to either a single dimensional array or null;

Hence the assignment a2[0] = a2[1] = a1, assigns the single dimensional array a1 to the 2nd dimensions(from right) of a2.

It will much helpful for you to understand using primitive arrays.

If its an array of objects, then even a1[0] will have the address of the object and another level of indirection.

Let me know if you get this.

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Thiyanesh,

I understood finally. Thankyou very much for the help.