wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arraay doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arraay doubt" Watch "Arraay doubt" New topic
Author

Arraay doubt

Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
class Dims {
public static void main(String[] args) {
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
System.out.println(b[1]);
} }


The output is 4
Can anyone explain. How?
Rakesh Ne
Greenhorn

Joined: Mar 12, 2007
Posts: 14
class Dims {
public static void main(String[] args) {
int[][] a = {{1,2,}, {3,4}};
//After the above line, these are the contents of a :
a[0] = {1,2}
a[1] = {3,4}
which is nothing but ->
a[0][0] = 1
a[0][1] = 2
a[1][0] = 3
a[1][1] = 4

int[] b = (int[]) a[1];
//After the above line b ={3,4}
i.e
b[0] = 3
b[1] = 4

// The below 2 lines are inconsequential
Object o1 = a;
int[][] a2 = (int[][]) o1;

System.out.println(b[1]);
} }

Hope it helps
Bijendra S. Rajput
Ranch Hand

Joined: Sep 19, 2006
Posts: 41
Hi Sangeev,

see here ....

int[] b = (int[]) a[1]; // in this line we are assigning a[1] array which has the value {3,4};

Now if i will print b[1], the value is 4, i think you got.


Thanks <br /> <br />Regards,<br />------------------------------<br />Bijendra S. Rajput<br />SCJP 1.5<br />------------------------------
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yuk, that's ugly code.


This creates a reference to two-dimensional array of integers. The contents of the the first element (element 0) is an array of integers with the values 1 and 2. The contents of the second element (element 1) is an array of integers with the values 3 and 4.


This creates a reference to a one dimensional array of integers. The contents of this array is the contents of the second element of the two dimensional array a. So that's 3 and 4.


This prints out the contents of the second element in array b, which is 4. All the other lines of code are just noise.

Did that make sense?!


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
// The below 2 lines are inconsequential
Object o1 = a;
int[][] a2 = (int[][]) o1;

these are the only lines that cause an error though
it gives a runtime exception...
i want to know why this does not work ?
otherwise the answer is 4 as told by you all

pl help
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No, there is nothing wrong with those lines (other than being pointless). Check your code. What RuntimeException do you see?
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from int[][] to Object
Cannot cast from Object to int[][]

i got this exception at the line Object o1 = a;
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
A sub-question

int[][] a = {{1,2,}, {3,4}};
After the above code, a is a two dimensional array and a[0], a[1] are one dimensional arrays. Am i right?

If I am right, is the cast necessary in the following line?
int[] b = (int[]) a[1];


live
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from int[][] to Object
Cannot cast from Object to int[][]

i got this exception at the line Object o1 = a;


That is a compile time exception, not a runtime exception.

The code is also valid Java, so I can't see why you would be getting that. A quick google suggests its not valid C# though. Are you using Java or C# to compile it?
[ March 27, 2007: Message edited by: Paul Sturrock ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by raja kanak:
A sub-question

int[][] a = {{1,2,}, {3,4}};
After the above code, a is a two dimensional array and a[0], a[1] are one dimensional arrays. Am i right?

If I am right, is the cast necessary in the following line?
int[] b = (int[]) a[1];


You are right, its not necessary. More noise to cloud the question I presume.
[ March 27, 2007: Message edited by: Paul Sturrock ]
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
@raja kanak

No, this cast is not required.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
i dont know it gives an error at my end
i use the eclipse ide to compile my codes

i think that it should not give an error because a 2d-array reference (a), points to an array object; which is after all an object.
so the line Object o1 = a; should not give an error according to me
you can give me a theoretical explanation for the same.
it will do

Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
@Prasad Shindikar
Do you have an Object class written by yourself? This might lead to such "funny" behaviour...
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Your understanding is correct. All arrays (regardless of their content) are actually Objects in java. So this:

is a perfectly legal thing to do.

very odd you are seeing that compilation error though.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
so that means that i can assign any array reference to an object reference.
i can also recast the same if i want

1 Object o1 = a;
2 int[][] a2 = (int[][]) o1;

can i also recast the same to say something like
int [] b = (int [])o1; assuming the first statement is true ?

pl explain

and one more thing i do not have an Object class of my own !!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes. Since arrays are Objects.

You could even do this:

and the compiler wouldn't complain. It would be a dumb thing to do though. You would get a ClassCastException when you ran it, since foo refers to an Integer not an array of integers.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
okay thanks !!!

i guess i have got the concept cleared.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
hi folks,

One more thing I would like to know from you:

int[][] a = {{1,2},{3,4}};
Object o1 =a;

o1 refers to a 2D array:
What if I want to assign the first of second dimension of the 2D array to one dimention array. I want to use o1 in this regard, so that my oneD array could be initialized with the first or second most dimention of the 2D array.

Please do consider the case! I have tried a lot.
This glass of Juice for the solution giver




Thanks,
cmbhatt


cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Here we go...

int[][] a = {{1,2},{3,4}};
Object oneD = a[1];
int[] b = (int[]) oneD;

Is this you want ?


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Originally posted by Chandra Bhatt:


o1 refers to a 2D array:
What if I want to assign the first of second dimension of the 2D array to one dimention array. I want to use o1 in this regard, so that my oneD array could be initialized with the first or second most dimention of the 2D array.


You cant, the references to the arrays must be of the same size dimensions.

If you can print the class of o1 out, it is:
[[I (indicating a two dim Integer array) which is not a sub class of one dim Integer.

You can prove this by using the instanceof operator, as int[] is not a super type of int[][] you cant assign int[][] to a int[] isnt even a sub type of Integer.

G
[ March 27, 2007: Message edited by: Gavin Tranter ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Alternative solution,

int[][] a = {{1,2},{3,4}};
Object o1 =a;
int[] b = (int[]) (( (int [][])o1 )[1])

I think this is the one. you are asking.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
"Originally Posted By Srinivas"

int[][] a = {{1,2},{3,4}};
Object oneD = a[1]; // Line 1
int[] b = (int[]) oneD; // Line 2


That could only give me a little relief not complete cure.
I wanted to make this in one line using casting. You got me correctly Srini but instead, can't we directly use o1 that is refering to the 2D array.

Thanks Srini,
cmbhatt
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Here is the one...


What about one glass of juice ?

Give me that "When you pass your SCJP 5.0 with 90%".
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


Posted By Srinivasan
Alternative solution,

int[][] a = {{1,2},{3,4}};
Object o1 =a;
int[] b = (int[]) (( (int [][])o1 )[1])

I think this is the one. you are asking.


Yeah! Absolutely, this is what I was searching for.

"For you Srini"

Thanks and Regards,
cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
What about one glass of juice ?

Give me that "When you pass your SCJP 5.0 with 90%".


Sure Srini, You are not much far from my location. Keep precious solution
giving on!



Thanks & Regards,
cmbhatt
[ March 27, 2007: Message edited by: Chandra Bhatt ]
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi Chandra,

Sure.
because,
I love this forum , ranchers out here.
because,
i love Java next to C.
because
i love to program better.
Sanjeev Narula
Greenhorn

Joined: Mar 16, 2007
Posts: 19
Actually these two lines:

Object o1 = a;
int[][] a2 = (int[][]) o1;

really scared me erlier..now i am having better understanding of these..Thanks all of you for participation
 
wood burning stoves
 
subject: Arraay doubt
 
Similar Threads
Conversion.....
Another Array question...
Object points to double array, yet when casting it to single array, it causes Runtime error
array
arrays