Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList.add overwrites?

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this code

To save the contents of a resultset rs into arraylist result. However, each result.add() overwrites all previous entries with the new one. So I get an arraylist filled with the last record pulled from the db. Why? If I only add() one value, such as add(dbrows[0]), it works fine.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You problem is not with ArrayList's add method, its with how you use your dbrows Object array. List.add() just adds an object in the next position in the List. You create one object and add it repeatedly to the list. You need to add different dbrows objects to the list. Otherwise (as you are seeing) every time you change its value, you change it for every occurance in the List.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic