• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array conversion Question from Dan's test

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code compiles & runs fine.o/p is 112.Can anybody explain how?
Thanks
Veena
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, is cloning tested in the exam ?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it.Thank you everyone.
Thanks again.
Veena
 
eat bricks! HA! And here's another one! And a tiny ad!
ScroogeXHTML - fast RTF to HTML and XHTML conversion
https://coderanch.com/t/749563/ScroogeXHTML-fast-RTF-HTML-XHTML
reply
    Bookmark Topic Watch Topic
  • New Topic