• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting 1Demsion Array from a 2Demsion Array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I get a 1 demsion array from a multi demsional array?
Something like this:
Object[] newArray = oldArray[1][];
My 2 d Array is a table and I want to put one row of it in a new array.
Thanks for your help
 
Matt Rothe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is:
Object[] singleArray = data[1];
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now similarly, I'd like to access the other dimension of the 2D array like so...
Object[] singleArray = twoD[][1];
But it looks like this isn't allowed.
I'd also like to avoid iterating through each array to pull the the 2nd element out and create a new array.
Are there any objects out there that allow the user to access a grid of objects in terms of rows or columns?
Thanks!
Spencer
 
Spencer J Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just thought i'd post my afterthoughts since my original posting...
I'm guessing that 2D array access like my_array[][2] is not allowed because of the way the memory is laid out...
Could it be that array access like my_array[0] (for a 2D array variable that is declared as my_array[][] = new String[4][3] for example) is permitted because the memory of the first dimension is laid out contiguosly, but my_array[][2] is not because the memory not laid out contiguously?
Any thoughts?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That pretty much sums it up -- of course, we're not supposed to be aware of how memory is laid out in Java
While thinking of a multi-dimensional array in terms of a matrix can be useful some (most) times, it does not really show the true nature of the beast, and you begin to fret over why you can't do things that it looks like you should be able to do.
I suggest thinking of an array as a row of boxes. A 2 dimensional array is a row of boxes, where each box contains its own row of boxes. However, each sub-row is entirely within the enclosing box; it does not border on any other sub-row. Therefore, the only way to get the second element of each sub-row is to go to each box in the first row, open it up, and then open up the second box therein.
I suppose that it is possible to write a Matrix class that would do what you want. It could use an array of arrays and do the iterating for you (hidden behind the scenes).
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Spencer Lee:
I just thought i'd post my afterthoughts since my original posting...
I'm guessing that 2D array access like my_array[][2] is not allowed because of the way the memory is laid out...
Could it be that array access like my_array[0] (for a 2D array variable that is declared as my_array[][] = new String[4][3] for example) is permitted because the memory of the first dimension is laid out contiguosly, but my_array[][2] is not because the memory not laid out contiguously?
Any thoughts?


Multi-dimensional arrays in Java are handled a little bit differently than multidimensional arrays in some other languages you may be familiar with. Java multi-dimensional arrays are actually arrays of arrays (of arrays of arrays...).
So while you may be used to thinking of a 2D array as a matrix, this isn't necessarily the case in Java. For example, initializing a 2d array in the following manner is perfectly legal:
String[][] sa = new String[5][];
Looking at the above, sa[0], sa[1], sa[2], etc... are each references to a String[]. In this case these references are actually null. If we do this:
sa[0] = new String[3];
sa[0] now contains a reference to a String[] of size 3, and sa[1] through sa[4] still point to null. The consequence of handling arrays in this manner is that, like I mentioned earlier, multi-dimensional arrays do not have to be matrix-like, but rather they can be "jagged" so to speak. Keeping in mind that we set sa[0] to be a reference to a String[] of size 3, the following is also perfectly legal:
sa[1] = new String[15];
This is what I meant by "jagged". sa[0] points to an array of size three while sa[1] points to an array of size 15! I could then go and fill in String values for sa[0][0], sa[0][1], sa[0][2], as well as values for sa[1][0] through sa[1][14].
You should realize by now that
sa[0][1] = "foo";
is the same as
String[] myArray = sa[0];
myArray[1] = "foo";

This should go some way to helping explain why you cannot make a reference such as sa[][2]. While a language like C maps out a contiguous block of memory and you could use pointer arithmetic to navigate between the memory addresses, the same wouldn't work in Java (even if you could theoretically access memory directly using pointers). More practically, you can't be assured that just because sa[0][2] is a valid reference, that sa[1][2] is also a valid reference, particularly since sa[1] might be less than size 3, in which case an ArrayIndexOutOfBoundsException would be thrown.
HTH
[ August 24, 2003: Message edited by: Jason Menard ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic