Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Array Conversion

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


In the line 2,it is said that the two dimensional array is converted to one dimensional array. If two dimensional array is converetd to one dimensiona array, how the values in 2-d array is given to 1-d array? in the above pgm the o/p is 112..how?
 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh, tricky one.

The 2 dimensional array is converted to an array of objects (3 objects).
Those objects are arrays again but that is not known for obj.

Later on , the 3 objects in the 1 dimensional array are casted again to int[] which is perfectly possible because an array is an object. Then the loop prints the values...

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


the last line makes an Object array with three references pointing to the ^ in



perhaps change the output line from
// System.out.print(ia[i]);
to this three lines:
System.out.println(java.util.Arrays.toString(ia));
boolean sameRef = ( obj[i] == a [i] ) ;
System.out.println(sameRef);


Then it prints:
[1, 2]
true
[0, 1, 2]
true
[-1, 0, 2]
true

The elements printed originally are in bold.

OK?



Yours,
Bu.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds simple.

Hmmm , the object array after "Object[] obj = (Object[])a.clone();" would look like : obj[0] = {1,2}
obj[1] = {0,1,2}
obj[2] = {-1,0,2}
now you are typcasting this object array in the loop "int[] ia = (int[])obj[i];"

so for i = 0 we will have int ia[] = {1,2} and when you say ia[0] output is =1.
similarly for i=1 we have int ia[] = {0,1,2} and ia[1] = 1 and you can draw similar conclusion for i=2.

I hope this answers your questions.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to understand a (N) Demensional any type of array is subclass of (N-m)Demensional Object array. Code below verify the statement could be true!???



it prints out:

true

[ December 13, 2006: Message edited by: Bob CHOI ]
[ December 13, 2006: Message edited by: Bob CHOI ]
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob CHOI:
How to understand a (N) Demensional any type of array is subclass of (N-m)Demensional Object array. Code below verify the statement could be true!???



it prints out:

true




You just need to remember two facts. In Java, (1) an array is an object, and (2) Java doesn't directly support N-dimensional arrays, it supports arrays of arrays. So...

So a 4 dimensional int array is really an array of an array of an array of an array of ints. And since an array of ints is an object, the 4D array of ints is also an array of an array of an array of objects. And since an array of objects is an object, the original 4D array of ints is also an array of an array of objects.

Henry
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,
This is really a very good explantion,hope one will not mess up the things when faces the questions on arrays of arrays... of arrays... of arrays..
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Henry.


Striving for digesting "array of array...."
 
Bob CHOI
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



You just need to remember two facts. In Java, (1) an array is an object, and (2) Java doesn't directly support N-dimensional arrays, it supports arrays of arrays. So...

So a 4 dimensional int array is really an array of an array of an array of an array of ints. And since an array of ints is an object, the 4D array of ints is also an array of an array of an array of objects. And since an array of objects is an object, the original 4D array of ints is also an array of an array of objects.

Henry




- an array is an object, in pseudo-Java

class X {}

Object o = new X[m]...[n]; //right is subtype of left

- an array can hold subtype of objects, in pseudo-Java

Object o[] = new X[m]...[n][n+1];

- "a (N) Demensional any type of array is subclass of (N-m)Demensional Object array" statement PROVED!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic