• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

doubt in arrays...

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SOURCE:www.danchisholm.net



output is:112...

i have not understand what is happened at line2?

can anyone explain me?

 
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at line 2, you are creating a clone of the array. then you assign that clone to an Object[]. As you might know, that int[] and every other primitive array is a sub-class of Object. But since this is a 2-D primitive array, so you are able to assign to an Object[]. You can think of a 1-D int array as an Object. So a 2-d int array as an array of 1-d int array. This is why the assignment is allowed...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok but which values can be assigned to 1-d Object array?

how values in 2-d array can b converted in to 1-d array?
 
Ankit Garg
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok but which values can be assigned to 1-d Object array?



1-d object array can store any type of an array except 1-d primitive arrays.

how values in 2-d array can b converted in to 1-d array?


values in 2-d array cannot be converted into 1-d array (at least directly without using any loop or something). I think you are confused here. The 2-d array converted into a 1-d array because we changed the type. We did not assign a 2-d int array into 1-d int array. We assigned a 2-d int array into a 1-d object array...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sry not cleared...

can you tell me the values in 1-d object array ?

 
Ankit Garg
Sheriff
Posts: 9683
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the object array here contains 1-d int arrays...
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here I have added extra displays to show what 'a' and 'obj' really look like inside....they are very similar.

So obj is really an array of 3 arrays.

The for loop executes 3 times.
Line 4 serves up the 1st, 2nd, + 3rd array from inside of obj.
Line 5 serves up 1st, 2nd, + 3rd value from the array produced in line 4.

Does this help?

int[][] a = {{1,2},{0,1,2},{-1,0,2}}; // 1
System.out.println("a = " + a.length); // 1.5
System.out.println("a = " + Arrays.toString(a[0])); // 1.6
System.out.println("a = " + Arrays.toString(a[1])); // 1.7
System.out.println("a = " + Arrays.toString(a[2])); // 1.8
Object[] obj = (Object[])a.clone(); // 2
System.out.println("obj = " + obj.length); // 2.5
System.out.println("obj = " + Arrays.toString((int[])obj[0])); // 2.6
System.out.println("obj = " + Arrays.toString((int[])obj[1])); // 2.7
System.out.println("obj = " + Arrays.toString((int[])obj[2])); // 2.8
for(int i = 0; i < obj.length; i++) { // 3
int[] ia = (int[])obj[i]; // 4
System.out.print(ia[i]); // 5
}

RESULT:

a = 3
a = [1, 2]
a = [0, 1, 2]
a = [-1, 0, 2]
obj = 3
obj = [1, 2]
obj = [0, 1, 2]
obj = [-1, 0, 2]
112

[ December 06, 2008: Message edited by: patrick avery ]
[ December 06, 2008: Message edited by: patrick avery ]
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Patrick and Ankit I found your explanation very helpful. But I don't understand why the output is 112. Could you please explain where that is coming from?
Thanks,
Meera
 
Ranch Hand
Posts: 246
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit and Patrick. I have been getting to understand the multidimensional array's topic.

Naveen Katoch
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ankit and patrick....it really helped me out ..
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by meera kanekal:
Patrick and Ankit I found your explanation very helpful. But I don't understand why the output is 112. Could you please explain where that is coming from?
Thanks,
Meera



Because, i doesn't remain constant. It changes from 0 to 1 to 2 with each iteration of the for loop. Therefore, obj[0] = 1, obj[1] = 1 and obj[2] = 2.

Does this make sense?
 
meera kanekal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Prateek am I correct in saying that
1 --> 0th element of ia[0] = [1,2]
1 --> 1st element of ia[1] = [0,1,2]
2 --> 2nd element of ia[2] = [-1,0,2]
Thanks,
Meera
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Basically, obj[i] contains the same element as ia[i] except for the fact that it needs to be cast to an int[].
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic