ArrayList_4.java - package jsjf import jsjf.exceptions...

This preview shows page 1 - 3 out of 5 pages.

package jsjf;import jsjf.exceptions.*;import java.util.*;/*** ArrayList represents an array implementation of a list. The front of* the list is kept at array index 0. This class will be extended* to create a specific kind of list.** @author Lewis and Chase* @version 4.0*/public abstract class ArrayList<T> implements ListADT<T>, Iterable<T>{private final static int DEFAULT_CAPACITY = 100;private final static int NOT_FOUND = -1;protected int rear;protected T[] list; protected int modCount;/*** Creates an empty list using the default capacity.*/public ArrayList(){this(DEFAULT_CAPACITY);}/*** Creates an empty list using the specified capacity.** @param initialCapacity the integer value of the size of the array list*/public ArrayList(int initialCapacity){rear = 0;list = (T[])(new Object[initialCapacity]);modCount = 0;}/*** Creates a new array to store the contents of this list with* twice the capacity of the old one. Called by descendant classes* that add elements to the list.*/protected void expandCapacity(){list = Arrays.copyOf(list, list.length*2); }/*** Removes and returns the last element in this list.** @return the last element in the list* @throws EmptyCollectionException if the element is not in the list*/public T removeLast() throws EmptyCollectionException{if (isEmpty())throw new EmptyCollectionException("ArrayList");T result;
rear--;result = list[rear];list[rear] = null;modCount++;return result;}/*** Removes and returns the first element in this list.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture