File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList" Watch "ArrayList" New topic


Arjun Sharma

Joined: Sep 12, 2006
Posts: 6
I have an arraylist with some elements in one method.
In a second method (independent from the first)Iam trying to display this list.
The result is only the last element is printed (number of times = list size)

Please Suggest a fix..
Ritika Saxena
Ranch Hand

Joined: Jul 02, 2006
Posts: 51
Are you using a List-iterator.
Please be more elaborate, the problem is not much clear.
Arjun Sharma

Joined: Sep 12, 2006
Posts: 6
sorry, please read this...
iam using getters and setter for addinga nd displaying the list.
and then yes there is an iterator for that list.

i even tried doing a loop on the list size, but i get the same result.
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Post your code please.

Stephen Foy - Microsoft Application Development Consultant
marc weber

Joined: Aug 31, 2004
Posts: 11343


Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here.

Thank you for your prompt attention, and enjoy the ranch!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Arjun Sharma

Joined: Sep 12, 2006
Posts: 6
skeleton of the code looks like this, iam trying to remove the complexity of reading the whole code that i wrote and just show you the arraylist implementation that am trying for.

[ September 12, 2006: Message edited by: Arjun Sharma ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

The problem is that there's only one Value object here. You keep changing its contents and re-adding it to the ArrayList, so the ArrayList contains many references to the same Value object. As a result, when you print all those out, you see the same thing printed many times. You need to create new Value objects to put in the list -- don't try to reuse the same one.

[Jess in Action][AskingGoodQuestions]
Arjun Sharma

Joined: Sep 12, 2006
Posts: 6
That worked!!
Thanks a bunch Ernest, Stephen, Ritika.
and Marc thanks for pointing out the name issue.
I agree. Here's the link:
subject: ArrayList
jQuery in Action, 3rd edition