/** * A complete implementation of the ArraListInterface * @author geoffreytowell * Created: Sep 2020 * Modified: gtowell, Feb 2021 */ public class ArraListIncomplete implements ArraListInterface { /** The default starting size of the undertlying array */ private static final int DEF_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 Object[] arra; /**** * Basic Constructor for the class *****/ public ArraListIncomplete() { this(DEF_CAPACITY); // note the -- in this case gratuitous -- use of this() } /** * Constructor to set up the underlying array to a specific startng size * Note that this comment violates encapsulation!!!! * * @param initialCapacity the starting size. */ public ArraListIncomplete(int initialCapacity) { arra = new Object[initialCapacity]; } /** * Add an item to the arraylist * * @param t the item to be added return true. */ public boolean add(Object t) { if (count >= arra.length) { grow(); } arra[count] = t; count++; return true; } /** * Increase the size of the underlying array */ private void grow() { Object[] newArra = new Object[(int) (arra.length * GROWTH_RATE)]; for (int i = 0; i < count; i++) { newArra[i] = arra[i]; } arra = newArra; } /** * 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 true if the items was added * @throws if the index is out of range (index < 0 || index > size()) */ public boolean add(int index, Object t) throws IndexOutOfBoundsException { if (count >= arra.length) { grow(); } for (int i = count; i <= index; i--) { arra[i + 1] = arra[i]; } arra[index] = t; count++; return true; } /** * 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 Object 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 { //TODO: WRITE ME } /** * 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, Object 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(Object 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 < arra.length; i++) { arra[i] = null; } count = 0; arra = new Object[DEF_CAPACITY]; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("["); sb.append(arra.length); 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); } }