• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing ArrayList of Objects  RSS feed

 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys when i got to print it keeps printing the same object over and over and over... heres the small amoutn of code that i am using.



pop removes the item and drops the counter. Just wondering if you guys had any ideas?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is "list" really an ArrayList? Because ArrayList has neither a "peek" nor a "pop" method. java.util.Stack does, though. It looks like you have some kind of stack rather than a list.

This ought to loop over some of the objects on the stack, but not all of them, because note that both "i" and list.size() change on every iteration of the loop. Everytime you call pop(), list.size() shrinks; but "i "also gets larger. You're therefore effectively doing only 1/2 of the stack. The normal way to iterator over a stack looks like

 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is list? I don't think it is an ArrayList because there are no pop() and peek() methods available. So how are pop() and peek() impelemented? I think there might be a problem with these two functions.

Also, you maight want to look at overriding the toString() method for your ColonyNode class. This will allow you to write something like

System.out.println("Backtracking to " + node);

This makes it easier to print it out rather than calling getX() and getY() every time.

Layne
 
Tempora Telora
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is actually an arraystack class that was created.. its just a stack but still
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean a org.apache.commons.collections.ArrayStack? Capitalization is always important in Java. Also when you're using classes which are not part of the standard JDK, it's helpful to tell people what classes you're talking about, to avoid confusion.

Did the previous posts solve your problem?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Randy Tatham:
it is actually an arraystack class that was created.. its just a stack but still


That was created by whom? Did you create it? Did your instructor? Did someone else? I don't see anything in the small amount of code you gave that would cause the behavior you described. Please provide some more details. Specifically, if you have the code for this ArrayStack class, we need to see the pop() and peek() methods.

Layne
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randy,

If you haven't got the idea already: the best way to describe a coding problem
is to include a minimal, runnable, example program that demonstrates your
problem. Forum members can copy, paste, compile and run in 10 seconds flat,
instead of scratching their heads over ArrayList vs peek and poke. Don't
post your actually code unless it is very short, because the longer the code
listing, the less likely anyone will slog through it. Creating an example
program also forces you to reduce your problem to its essentials, and often
you discover your error doing this, so you solve your own problem!

After all that, here's a sample program. Do you see why the first list
contains 10 copies of the same object and the second list doesn't?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!