Source of ArrayListWithListIterator.java


  1: import java.util.Arrays;
  2: import java.util.Iterator;
  3: import java.util.ListIterator;
  4: import java.util.NoSuchElementException;
  5: /**
  6:    A class that implements the ADT list by using an array.
  7:    The list has entries that are numbered beginning at 1.
  8:    The list has an iterator that implements the interface ListIterator.
  9:    Iterator positions (indexes) are numbered beginning at 0.
 10:    
 11:    @author Frank M. Carrano
 12:    @author Timothy M. Henry
 13:    @version 5.0
 14: */
 15: public class ArrayListWithListIterator<T>
 16:              implements ListWithListIteratorInterface<T>
 17: {
 18:    private T[] list;  // Array of list entries; ignore list[0]
 19:    private int numberOfEntries; 
 20:    private boolean integrityOK;
 21:    private static final int DEFAULT_CAPACITY = 25;
 22:         private static final int MAX_CAPACITY = 10000;

 24:    public ArrayListWithListIterator()
 25:    {
 26:       this(DEFAULT_CAPACITY);
 27:    } // end default constructor

 29:    public ArrayListWithListIterator(int initialCapacity)
 30:    {
 31:       integrityOK = false;
 32:       
 33:       // Is initialCapacity too small?
 34:       if (initialCapacity < DEFAULT_CAPACITY)
 35:          initialCapacity = DEFAULT_CAPACITY;
 36:       else // Is initialCapacity too big?
 37:          checkCapacity(initialCapacity);
 38:       
 39:       // The cast is safe because the new array contains null entries
 40:       @SuppressWarnings("unchecked")
 41:       T[] tempList = (T[])new Object[initialCapacity + 1];
 42:       list = tempList;
 43:       numberOfEntries = 0;
 44:       integrityOK = true;
 45:    } // end constructor
 46:   
 47:    /* < Implementations of the methods of the ADT list go here;
 48:         you can see them in Chapter 11, beginning at Segment 11.5. */
 49:    
 50:    public ListIterator<T> getIterator()
 51:    {
 52:       return new ListIteratorForArrayList();
 53:    } // end getIterator
 54:    
 55:    public Iterator<T> iterator()
 56:    {
 57:       return getIterator();
 58:    } // end iterator
 59:    
 60:    private class ListIteratorForArrayList implements ListIterator<T>
 61:    {
 62:       // The details of this class begin with Segment 13.24.
 63:    } // end ListIteratorForArrayList
 64: } // end ArrayListWithListIterator