/** * @author geoffreytowell A complete implementation of the ArraListInterface * discussed in class */ public class ArraListInc implements ArraListInterface { /** The max number of items in the undertlying array */ private int capacity = 10; /** When the underlying array need to be expanded, the multiplier */ private static final double GROWTH_RATE = 1.618033; // the golden mean /** The actual number of items stored */ private int count; /** The array in which all the data is actually stored */ private T[] arra; /** * Basic Constructor. Just initializes the underlying array */ @SuppressWarnings("unchecked") public ArraListInc() { arra = (T[]) new Object[capacity]; } /** * Constructor to set up the underlying array to a specific startng size * * @param initialCapacity the starting size. */ @SuppressWarnings("unchecked") public ArraListInc(int initialCapacity) { arra = (T[]) new Object[initialCapacity]; capacity = initialCapacity; } /** * Add an item to the arraylist * * @param t the item to be added return true. */ public boolean add(T t) { return false; } /** * Add an item to the array list at a particular location. Inserts the specified * element at the specified position in this list. Shifts the element currently * at that position (if any) and any subsequent elements to the right (adds one * to their indices). * * @param index The item to be added * @param t the location to add the item at * @return * @throws if the index is out of range (index < 0 || index > size()) */ public boolean add(int index, T t) throws IndexOutOfBoundsException { return false; } /** * Get an item from the array list * * @param index the location of the item to be retrieved. * @return the specified item (or null if the array list does not have that many * items) * @throws if the index is out of range (index < 0 || index > size()) */ public T get(int index) throws IndexOutOfBoundsException { if (index > count) { throw new IndexOutOfBoundsException("Can only add where there are already items"); } if (index < 0) { throw new IndexOutOfBoundsException("Canot store to negative location"); } return arra[index]; } /** * Removes the element at the specified position in this list. Shifts any * subsequent elements to the left (subtracts one from their indices). * * @param index the index of the element to be removed */ public void remove(int index) throws IndexOutOfBoundsException { if (index > count) { throw new IndexOutOfBoundsException("Can only add where there are already items"); } if (index < 0) { throw new IndexOutOfBoundsException("Canot store to negative location"); } for (int i = index; i < (count - 1); i++) { arra[i] = arra[i + 1]; } count--; } /** * Replaces the element at the specified position in this list with the * specified element. * * @param index place to put teh element * @param t the element to be put into the array * @return * @throws IndexOutOfBoundsException if the index is out of range (index < 0 || * index > size()) */ public boolean set(int index, T t) throws IndexOutOfBoundsException { if (index > count) { throw new IndexOutOfBoundsException("Can only add where there are already items"); } if (index < 0) { throw new IndexOutOfBoundsException("Canot store to negative location"); } arra[index] = t; return true; } /** * Returns the number of elements in this list. * * @return the number of elements in this list. */ public int size() { return count; } /** * Returns the index of the first occurrence of the specified element in this * list, or -1 if this list does not contain the element. More formally, returns * the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or * -1 if there is no such index. * * @param t the item to be found * @return the index of the first occurrence of the specified element in this * list, or -1 if this list does not contain the element */ public int indexOf(T t) { for (int i = 0; i < count; i++) { if (arra[i] == t) return i; } return -1; } /** * Removes all of the elements from this list. The list will be empty after this * call returns. */ public void clear() { for (int i = 0; i < capacity; i++) { arra[i] = null; } count = 0; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); sb.append(capacity); sb.append(":"); sb.append(count); sb.append(" "); for (int i = 0; i < count; i++) { sb.append(String.format("{%d, %s}", i, arra[i])); if (i < (count - 1)) sb.append(", "); } sb.append("]"); return sb.toString(); } /** * A Driver with a set of illustrative tests showing te use fo the the class and * that the functions of the class actually work * * @param args ignored */ public static void main(String[] args) { ArraList test = new ArraList<>(); for (int i = 0; i < 5; i++) { test.add(i); } System.out.println(test); System.out.println("Value at pos 3 " + test.get(3)); for (int i = 0; i < 7; i++) { test.add(i); } System.out.println(test); Integer ii30 = Integer.valueOf(30); test.add(4, ii30); test.set(10, 100); System.out.println(test); test.remove(0); int fi = test.indexOf(ii30); System.out.println(test); System.out.println(fi); } }