Forums Register Login

Array Problem

+Pie Number of slices to send: Send
From Dan Chisholm


I thought it will be a runtime exception.
but the answer says
Prints null
and runtime exception

Can anybody explains why is it so?
[ December 15, 2006: Message edited by: Sanjeev Kumar Singh ]
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

Hemadri wrotes..
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 ]
+Pie Number of slices to send: Send
Sanjeev,

Can you explain it, if you understood??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thiyanesh, Can you explain it very broadly?? I got confused.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thiyanesh, thanks for the explanation. Can you also please explain once again the assignment for the arrays. I'm only partially understanding your explanation??
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Can you just give a clear picture of what the line below means,

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

I'm not familiar with pointers...
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Thiyanesh,

I understood finally. Thankyou very much for the help.
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 982 times.
Similar Threads
arrays: IndexOutOfBounds exception
I got a question about [ ] [ ] [ ].
Arrays doubt
multi-dimension array question - dan's exam
MultiDimensional Arrays
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:44:37.