File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes ArrayList.add overwrites? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList.add overwrites?" Watch "ArrayList.add overwrites?" New topic
Author

ArrayList.add overwrites?

Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
Ah, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ArrayList.add overwrites?
 
Similar Threads
stuck in a loop
How to sort ip addresses?
How to subtract two arrayLists?
ArrayList.add() in a for loop, will the order of the items always be the same?
How to put a list of data retrieve from database into arraylist