programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Array conversion Question from Dan's test

Ranch Hand
Posts: 469
• Number of slices to send:
Optional 'thank-you' note:

The above code compiles & runs fine.o/p is 112.Can anybody explain how?
Thanks
Veena

Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:
By the way, is cloning tested in the exam ?

Ranch Hand
Posts: 247
• Number of slices to send:
Optional 'thank-you' note:
Hi Veena,

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.

Ranch Hand
Posts: 1561
• Number of slices to send:
Optional 'thank-you' note:

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: 469
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by cyril vidal:
Hi Veena,

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

Greenhorn
Posts: 17
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 19
• Number of slices to send:
Optional 'thank-you' note:
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: 469
• Number of slices to send:
Optional 'thank-you' note:
I got it.Thank you everyone.
Thanks again.
Veena

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?