Collections Framwork

Iterators and List Iterators

Any class in Java that implements the Collections interface can have an iterator. Furthermore, classes that implement the List interface can also have a specialised list iterator.

List Iterator Project

  • Modify your Dog code such that the toString for the Dog class prints the name, age and weight of the Dog all on one line.

  • Using the LinkedList class in the standard Java libraries (not your own version of the LinkedList class), create and populate a list of 9 Dog objects. Be sure to give each dog a unique name, age and weight.

  • Name the dogs such that the first dog, last dog, and one of the middle dogs has a name starting with capital "A". Make sure no other dogs have names starting with "A".

  • Set the name of the second and third dogs on the list to start with the letter "B". Make sure no other dogs have names starting with "B".

  • Now do the following in your main method:

  1. Create a list iterator for your list.

  2. Parse the entire list forwards and print each dog, one per line.

  3. Once your iterator has gotten to the end of your list, parse the list backwards, again printing each dog one per line.

  4. Now parse the list forwards again while removing all the dogs that start with the letter "A".

  5. Print the entire list using the LinkedList's built-in toString method.

  6. Now parse the list backwards again while removing all the dogs that have names starting with "B".

  7. Again, print the entire list using the LinkedList's built-in toString method.

  8. Create a new dog and assign it to a new variable called tempDog.

  9. Finally, parse the list forward one more time while replacing the dogs with even indexes with your tempDog. (Use the iterator set command to do this).

  10. Print the final list.