Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

2d array issues.

 
Wayne Sheets
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to read a fixed size csv file.
Please check the code comments.




Any help?

Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20997
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan[Or just create the "inner" String array inside the loop. [/quote wrote:

I used this method. Its works great... thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic