• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java iterate over collection not working as expected  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am currently working on a Java project, below are my attempts at coding so far:




I currently cannot get the printAllAnimals() method to work as it should.

When executing the method printAllAnimals(), it does not do anything, when it is supposed to use the Collection object c, so that animals stored in the zoo can easily be checked

Any help would be greatly appreciated as I am confused.
 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know about others, but it would help me to see Animals.java.
 
lartax bob
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I don't know about others, but it would help me to see Animals.java.


Here is animals.Java:

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "it is supposed to use the Collection object c"? You declare that variable in the printAllAnimals() method, but then you never actually use it anywhere.

There are some other things that are strange about that method that make it difficult to see what you're trying to do. Why are you creating a second TreeMap sortedItems, and then (again) never using it? And then you're getting the value from the animals TreeMap, which should be an Animal object (according to line 8), but you're trying to treat it like a List of Animals.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There is something not quite right about an Animal object whose constructor can be called like new Animal("lion", "Simba", this);
Which zoo an animal is in is not part of the Animal class; it is for the Zoo object to record its Animals, not vice versa.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!