Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arraay doubt

 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?!
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is nothing wrong with those lines (other than being pointless). Check your code. What RuntimeException do you see?
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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];
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@raja kanak

No, this cast is not required.
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Prasad Shindikar
Do you have an Object class written by yourself? This might lead to such "funny" behaviour...
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks !!!

i guess i have got the concept cleared.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandra,

Here we go...

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

Is this you want ?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic