Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

2d array issues.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to read a fixed size csv file.
Please check the code comments.




Any help?

Thanks.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Hug your destiny! And hug this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic