wood burning stoves 2.0*
The moose likes Java in General and the fly likes 2d array issues. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "2d array issues." Watch "2d array issues." New topic
Author

2d array issues.

Wayne Sheets
Greenhorn

Joined: Feb 27, 2012
Posts: 7
I'm trying to read a fixed size csv file.
Please check the code comments.




Any help?

Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18761
    
  40

Wayne Sheets wrote:I'm trying to read a fixed size csv file.
Please check the code comments.




Any help?

Thanks.


Remember that java technically doesn't have 2D arrays. It has arrays of arrays. So, this statement...



Is assigning the element of the outer array to point to the row array -- the previous row is now eligible for GC. And of course, by the time the loop is done, all the elements will refer to the same row array.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Wayne Sheets wrote:



The above line create one String array object.





The above part of your loop puts String references into the elements of that one String array that you created.



The above puts a reference to that one single String array that was ever created into the current element of the data array.

In other words, you're only ever creating one array of Strings, and then you keep replacing its contents with the new Strings from each line, and then all the "outer" elements of your 2D array point to that same single String[] object.

You need to create a new String[] for each row.


When you do x = y with reference variables in Java, it only ever copies the reference, never the object, so the result of that assignment will simply be two reference variables pointing to the same object.
Wayne Sheets
Greenhorn

Joined: Feb 27, 2012
Posts: 7
Henry Wong wrote:Remember that java technically doesn't have 2D arrays. It has arrays of arrays. So, this statement...



Is assigning the element of the outer array to point to the row array -- the previous row is now eligible for GC. And of course, by the time the loop is done, all the elements will refer to the same row array.

Henry


Thanks for the reply. I understand what you're saying.

Whats the best way then to transfer the contents of row into data?
Wayne Sheets
Greenhorn

Joined: Feb 27, 2012
Posts: 7
Jeff Verdegan wrote:
The above puts a reference to that one single String array that was ever created into the current element of the data array.

In other words, you're only ever creating one array of Strings, and then you keep replacing its contents with the new Strings from each line, and then all the "outer" elements of your 2D array point to that same single String[] object.

You need to create a new String[] for each row.


When you do x = y with reference variables in Java, it only ever copies the reference, never the object, so the result of that assignment will simply be two reference variables pointing to the same object.


That makes a little more sense. Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18761
    
  40

Wayne Sheets wrote:Thanks for the reply. I understand what you're saying.

Whats the best way then to transfer the contents of row into data?



The arraycopy() method of the System class, is good for doing this.

Henry
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Henry Wong wrote:
Wayne Sheets wrote:Thanks for the reply. I understand what you're saying.

Whats the best way then to transfer the contents of row into data?



The arraycopy() method of the System class, is good for doing this.

Henry


Or just create the "inner" String array inside the loop.
Wayne Sheets
Greenhorn

Joined: Feb 27, 2012
Posts: 7
Jeff Verdegan[Or just create the "inner" String array inside the loop. [/quote wrote:

I used this method. Its works great... thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 2d array issues.