Sept 30 The list Interface The List interface is implemented by classes that represent an ordered collecction fo elements such as numbers or strings. Four of the List interface methods--iterator, listIterator, size, and add--are shown in figure 5.5. The iterator and listIterator methods return an object that can be used to cycle through every element in the list. This is called iteration. The size method simply returns the number of elements in the list. The add method adds the element given as a parameter to the end of the list. ListIterator listIterator() // returns a list iterator of the leements in the list. Iterator iterator() // returns an iterator of the elements in the list. int size() // returns the number of the leemnts in the list. void add(Object obj) // adds the specificed element to the end of the list. These methods define the interface for the list. The underlying implementation is defined by classes that implement the List interface. Different classes have different implementations. ----------- the Iterator and ListIterator interfaces The Iterator interface is used by classes that represnt a collection of objects such as a list, giving us a way to move through the collection one object at a time. The Iterator interface is not used to represent the list itself, it merely represents a way to move through the elements of the list. The two basic methods in the Iterator interface are hasNext, which returns a boolean result, and next, which returns an object. Neither of these methods takes any parameters. The hasNext method returns true if there are itesms left to process, and next returns the next object. It is up to the designer to decide in what order objects will be delivered by the next method. The methods of the Iterator interface are listed in figure 5.6. The next method does not remove the object from the collection; it just returns a reference to it. The Iterator interface also has a method called remove, which takes no parameters and has a void return type. A call to the remove method removees the object that was momst recently returned by the next method from the collection. The ListIterator interface contains the methods of the Iterator interface plus a few more, including add and set. Figure 5.7 lists some of the methods of the ListIterator interface. The add and set methods allow new elements tobe added to the list. We will learn more about lists and iterators in Chapter 6. boolean hasNext() // returns true if the executing object contains one or more objects that have not been returned by the next method. Object next() // returns a reference to the next object in the iterator void remove() // removes the item most recentyl returned by the next method from the underlying collection boolean hasNext() // returns true if the executing object contains one or more objects that have not been returned by the next method. Object next() // returns a reference to the next object in the iterator. void remove() // removes the item most recently returned by the next method from the underlying collection void add(Object obj) // Inserst the leemnt obj into the list immediately after the last element that was returned by the next method. void set(Object obj) // Replaces the last element returned by next with the element obj.