• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
Arjun Sharma
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using a List-iterator.
Please be more elaborate, the problem is not much clear.
 
Arjun Sharma
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your code please.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Arjun Sharma
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Sharma
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic