aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Problem" Watch "Array Problem" New topic
Author

Array Problem

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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 ]

~Sanjeev Singh<br />SCJP 1.5
hemadri raju
Greenhorn

Joined: Mar 28, 2006
Posts: 15
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
hemadri raju
Greenhorn

Joined: Mar 28, 2006
Posts: 15
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
hemadri raju
Greenhorn

Joined: Mar 28, 2006
Posts: 15
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

Joined: Nov 01, 2006
Posts: 381
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 ]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Sanjeev,

Can you explain it, if you understood??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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.
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
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

Joined: Nov 01, 2006
Posts: 381
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Mar 19, 2006
Posts: 142
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Thiyanesh, Can you explain it very broadly?? I got confused.
S Thiyanesh
Ranch Hand

Joined: Mar 19, 2006
Posts: 142
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Mar 19, 2006
Posts: 142
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

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

Joined: Nov 01, 2006
Posts: 381
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

Joined: Mar 19, 2006
Posts: 142
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 Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9427
    
    2

Thiyanesh,

I understood finally. Thankyou very much for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array Problem