Skip to main content
ICT
Lesson AB27 - Java Lists and Iterators
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

D. Traversing a List using Iterator or ListIterator Objects page 6 of 9

  1. A traversal of a list is an operation that visits all the elements of the list in sequence and performs some operation.

  2. An iterator is an object associated with the list. When an iterator is created, it points to a specific element in the list, usually the first. The iterator provides methods to check whether there are more elements to be visited, to obtain the next element, and to remove the current element. This makes it easy to traverse a list. Using an iterator helps prevent OBOBs ('Off-By-One-Bugs': attempts to access an element beyond the length of the list) because the iterator knows how big the list is.

  3. In Java, iterators are defined by the library interface, java.util.Iterator.

  4. Iterator defines three basic methods:

    Object next()
    // Returns the next element in the iteration

    boolean hasNext()
    // Returns true if the iteration has more elements

    void remove()
    // Removes the last element returned by next from the list

  5. A list traversal would be implemented using an iterator as follows.

    LinkedList <String> list = new LinkedList <String>();
    // Add values to the list
    ...

    Iterator <String> iter = list.iterator();
    while (iter.hasNext()){
      System.out.println(iter.next());
    }

    Note that the list provides the iterator.

  6. A limitation of the Iterator interface is that an iterator always starts at the beginning of the list and can only move forward.

  7. A more comprehensive ListIterator object is returned by List’s listIterator method. ListIterator extends Iterator. A ListIterator can start at any specified position in the list and can proceed forward or backward. For example:

    ListIterator <String> listIter =
    list.listIterator(list.size());
    while (listIter.hasPrevious()){
      String value = listIter.previous();
      // process value
      ...
    }

  8. Some useful ListIterator methods are summarized below:

    Object next()
    // Returns the next element in the iteration.

    void add(Object x)
    // Inserts the argument into the list being iterated over.

    void set(Object x)
    // Replaces the last element returned by next by the argument.

  9. The following two examples illustrate one of the advantages of iterators.

    Example 1:

    LinkedList <String> list = new LinkedList <String>();
    // Add values to the list

       list.add("John");
       list.add("Ann");
       list.add("Betsy");
       list.add("Nancy");

       for(int i = 0; i < list.size(); i++){
          String value = list.get(i);
          System.out.println(value);
          if(value.equals("Ann"))
             list.remove(i);
       }

    Run Output:

    John
    Ann
    Nancy

    Example 2:

    LinkedList <String> list2 = new LinkedList <String>();
    // Add values to the list

       list2.add("John");
       list2.add("Ann");
       list2.add("Betsy");
       list2.add("Nancy");
       Iterator <String> listIter = list2.iterator();
       while (listIter.hasNext()){
          String value = listIter.next();
          System.out.println(value);
          if(value.equals("Ann"))
             listIter.remove();
       }

    Run Output:

    John
    Ann
    Betsy
    Nancy

    In Example 1, the index is external to the list, so after removing Ann, i is incremented but the elements of the list have changed positions and Betsy is skipped. In Example 2, the iterator keeps track of the positions of the elements after Ann is removed and Betsy is not skipped.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.