ArrayList.java.html

  1  /**
  2     This is a simplified implementation of an array list.
  3  */
  4  public class ArrayList
  5  {
  6     private Object[] elements;
  7     private int currentSize;
  8  
  9     /**
 10        Constructs an empty array list.
 11     */
 12     public ArrayList() 
 13     {
 14        final int INITIAL_SIZE = 10;
 15        elements = new Object[INITIAL_SIZE];
 16        currentSize = 0;
 17     }
 18  
 19     /**
 20        Gets the size of this array list.
 21        @return the size
 22     */
 23     public int size() { return currentSize; }
 24  
 25     /**
 26        Throws an IndexOutOfBoundsException if the checked index is out of bounds
 27        @param n the index to check
 28     */
 29     private void checkBounds(int n)
 30     {
 31        if (n < 0 || n >= currentSize) 
 32        {
 33           throw new IndexOutOfBoundsException();
 34        }
 35     }
 36  
 37     /**
 38        Gets the element at a given position.
 39        @param pos the position
 40        @return the element at pos
 41     */
 42     public Object get(int pos)
 43     {
 44        checkBounds(pos);
 45        return elements[pos];
 46     }
 47  
 48     /**
 49        Sets the element at a given position.
 50        @param pos the position
 51        @param element the new value
 52     */
 53     public void set(int pos, Object element)
 54     {
 55        checkBounds(pos);
 56        elements[pos] = element;
 57     }
 58  
 59     /**
 60        Removes the element at a given position.
 61        @param pos the position
 62        @return the removed element
 63     */
 64     public Object remove(int pos)
 65     {      
 66        checkBounds(pos);
 67  
 68        Object removed = elements[pos];
 69  
 70        for (int i = pos + 1; i < currentSize; i++)
 71        {
 72           elements[i - 1] = elements[i];
 73        }
 74  
 75        currentSize--; 
 76  
 77        return removed;
 78     }
 79  
 80     /**
 81        Adds an element after a given position.
 82        @param pos the position
 83        @param newElement the element to add
 84     */
 85     public boolean add(int pos, Object newElement)
 86     {
 87        growIfNecessary();
 88        currentSize++;
 89  
 90        checkBounds(pos);
 91  
 92        for (int i = currentSize - 1; i > pos; i--)
 93        {
 94           elements[i] = elements[i - 1];
 95        }
 96  
 97        elements[pos] = newElement;
 98        return true;
 99     }
100  
101     /**
102        Adds an element after the end of the array list
103        @param newElement the element to add
104     */
105     public boolean addLast(Object newElement)
106     {
107        growIfNecessary();
108        currentSize++;
109        
110        elements[currentSize - 1] = newElement;
111        return true;
112     }
113  
114     /**
115        Grows the elements array if the current size equals the capacity.
116     */
117     private void growIfNecessary()
118     {
119        if (currentSize == elements.length)
120        {
121           Object[] newElements = new Object[2 * elements.length];
122           for (int i = 0; i < elements.length; i++) 
123           { 
124              newElements[i] = elements[i]; 
125           }      
126           elements = newElements;
127        }
128     }
129  }