This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array conversion Question from Dan's test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array conversion Question from Dan Watch "Array conversion Question from Dan New topic
Author

Array conversion Question from Dan's test

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442

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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
bennido kool kat
Greenhorn

Joined: Jun 30, 2003
Posts: 23
By the way, is cloning tested in the exam ?
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
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.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
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


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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

Joined: Jul 08, 2003
Posts: 17
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

Joined: Jul 02, 2003
Posts: 247
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

Joined: Jul 15, 2003
Posts: 19
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


pradeep
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
I got it.Thank you everyone.
Thanks again.
Veena
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array conversion Question from Dan's test
 
Similar Threads
Dan exam doubt in arrays
Array of Arrays ???
clone from Dans
doubt in arrays...
Array Conversion