aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList" Watch "ArrayList" New topic
Author

ArrayList

Arjun Sharma
Greenhorn

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
Greenhorn

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
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"rakshas,"

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!

-Marc


"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
sscce.org
Arjun Sharma
Greenhorn

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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: ArrayList