This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
How can I get a 1 demsion array from a multi demsional array? Something like this: Object newArray = oldArray; My 2 d Array is a table and I want to put one row of it in a new array. Thanks for your help
Now similarly, I'd like to access the other dimension of the 2D array like so... Object singleArray = twoD; 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
Joined: Mar 19, 2003
I just thought i'd post my afterthoughts since my original posting... I'm guessing that 2D array access like my_array is not allowed because of the way the memory is laid out... Could it be that array access like my_array (for a 2D array variable that is declared as my_array = new String for example) is permitted because the memory of the first dimension is laid out contiguosly, but my_array is not because the memory not laid out contiguously? Any thoughts?
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).
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
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 is not allowed because of the way the memory is laid out... Could it be that array access like my_array (for a 2D array variable that is declared as my_array = new String for example) is permitted because the memory of the first dimension is laid out contiguosly, but my_array 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; Looking at the above, sa, sa, sa, etc... are each references to a String. In this case these references are actually null. If we do this: sa = new String; sa now contains a reference to a String of size 3, and sa through sa 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 to be a reference to a String of size 3, the following is also perfectly legal: sa = new String; This is what I meant by "jagged". sa points to an array of size three while sa points to an array of size 15! I could then go and fill in String values for sa, sa, sa, as well as values for sa through sa. You should realize by now that sa = "foo"; is the same as String myArray = sa; myArray = "foo"; This should go some way to helping explain why you cannot make a reference such as sa. 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 is a valid reference, that sa is also a valid reference, particularly since sa might be less than size 3, in which case an ArrayIndexOutOfBoundsException would be thrown. HTH [ August 24, 2003: Message edited by: Jason Menard ]