• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help on two-dimensional array  RSS feed

 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a piece of old code that I have:



I've some confusion on line#9.

If I am not mistaking, java allows to convert one-dimensional array of any type to an Object type and hence converting two-dimensional String array to single-dimensional Object array is syntactically ok.
But the confusion is what is the meaning of this?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A "two-dimensional array" in Java is really a one-dimensional array of one-dimensional arrays; i.e, if you declare

int[][x] x = new int[2][2];

you get three objects: two one-dimensional int[]'s, each two elements long; and one one-dimensional int[][] -- a one-dimensional array that holds two one-dimensional arrays of int. It's this third object that x points to, and it's also this third object that you can cast to Object[], since an int[] is an Object.

Make sense?
 
Ankit Doshi
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so based on the above code:

Variable "rows" is an ArrayList, each element of which contains an array.

When we call rows.toArray(), it should return a one-dimensional array, each element of which is again a one-dimensional array. And this "one-dimensional array, each element of which is again a one-dimensional array" would be stored in the variable named "data" which again is a two-dimensional array. Is this understanding correct?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!