• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array conversion Question from Dan's test

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code compiles & runs fine.o/p is 112.Can anybody explain how?
Thanks
Veena
 
bennido kool kat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, is cloning tested in the exam ?
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena,
What I understood about this example:

this a for effect to clone the double dimensional array referenced by i to a single dimensional one of type object.
Now, each element of obj is itself an int array:
obj[0] = {1,2}
obj[1] = {0,1,2}
obj[2] = {-1,0,2}

The explicit cast into an int array avoids a compiler error (an implicit cast can't be done from a class -here Object because obj[i] is of typer Object- to an array) and because obj[i] is an array of type integer, there's also no runtime error.
so first loop:
int [] ia = {1,2};//ia[0] = 1
System.out.println(1);
second loop:
int [] ia = {0,1,2}; //ia[1] = 1
System.out.println(1);
third loop:
int [] ia = {-1,0,2}; //ia[2] = 2
System.out.println(2);
So output gives: 112
Hope this helps,
Cyril.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bennido kool kat:
By the way, is cloning tested in the exam ?

No. It is not tested. But it's good to know it
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by cyril vidal:
Hi Veena,
What I understood about this example:

this a for effect to clone the double dimensional array referenced by i to a single dimensional one of type object.
Now, each element of obj is itself an int array:
obj[0] = {1,2}
obj[1] = {0,1,2}
obj[2] = {-1,0,2}

The explicit cast into an int array avoids a compiler error (an implicit cast can't be done from a class -here Object because obj[i] is of typer Object- to an array) and because obj[i] is an array of type integer, there's also no runtime error.
so first loop:
int [] ia = {1,2};//ia[0] = 1
System.out.println(1);
second loop:
int [] ia = {0,1,2}; //ia[1] = 1
System.out.println(1);
third loop:
int [] ia = {-1,0,2}; //ia[2] = 2
System.out.println(2);
So output gives: 112
Hope this helps,
Cyril.


cyril,
Thanks for explaining.I understood part of it.But I got confused with this line
int[] ia = (int[])obj[i];
The above line assigns 2 dimensional array to 1 dimensional array,which should give compiler error.why it is not giving compiling error?
Veena
 
Saravanakumar Rajmohan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena,
int[] ia = (int[])obj[i];
the above code does not assign a 2 dimensional array to single dimensional array but assigns the i element of a two dimensional array to a single dimensional array.
two dimensional array is nothing but a single dimension array whose elements are single dimensional array( an array of single dimensional array) and it is valid
i hope u understood
thank u
Saravanakumar R
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Veena,

But I got confused with this line
int[] ia = (int[])obj[i];
The above line assigns 2 dimensional array to 1 dimensional array,which should give compiler error.why it is not giving compiling error?

The type of reference of obj[i] at compile time is not a two or one dimensional array, but Object. And you may do an explicit cast from a class Object to an array.
Hope this helps,
Cyril.
 
pradeepbill arumalla
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,
we have...
obj[0] = {1,2}
obj[1] = {0,1,2}
obj[2] = {-1,0,2}
here in the for loop ...
for(int i = 0;i<obj.length; i++) {
int[] ia = (int[])obj[i];
}
the obj[i] has the address ...try printing it...it is assigned to an integer array...and now ...its as simple as a single dimensional array ...to be printed are the index valued 0,1,2 elements.
thanks
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.Thank you everyone.
Thanks again.
Veena
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic