Well, if it has many Animals
, you will want to return an Iterator<Animal>
. Try this code, for starters:
public Iterator<Animal> iterator() {
return new Iterator<Animal>() {
public boolean hasNext() {
// your code here
}
public Animal next() {
// your code here
}
public void remove() {
// you really don't need to do anything here unless you want to
}
}
}
If you already store the Animal
s in an array, you can use just one line of code:
public Iterator<Animal> iterator() {
return Arrays.asList(yourAnimalArray).iterator();
}
And if you store it in any type of Collection<Animal>
(such as an ArrayList
):
public Iterator<Animal> iterator() {
return yourAnimalCollection.iterator();
}