/** * @author geoffreytowell * File ArraList.java * Date: 9/11/2019 * *In Class implementation of ArrayList. Use this for reference only. * The implementation in java.util.ArrayList is superior * Completed */ public class ArraList implements ArraListInterface { /** * The default initial size of the backing array **/ private static final int DEFAULT_CAPACITY = 10; /** * The rate at which the backing array grows **/ private static final double GROWTH_RATE = 1.618033; // the golden mean /** * The number of items stored **/ private int count; /** * The backing array for the data structure **/ private T[] arra; /** * Create an ArraeList of the default size **/ public GTList() { this(GTList.DEFAULT_CAPACITY); } /** * Create an array list with the given initial capacity * @param iCapacity -- the size of the initial backing array * @throws IllegalArgumentException if the initial capacity is less than or equal to zero **/ @SuppressWarnings("unchecked") public GTList(int iCapacity) throws IllegalArgumentException { if (iCapacity<=0) throw new IllegalArgumentException(); arra = (T[])new Object[iCapacity]; } /** * Add an element * @param T the element to be added * @return always returns true **/ @Override public boolean add(T t) { count++; if (count>=arra.length) grow(); arra[count]=t; return true; } @SuppressWarnings("unchecked") private void grow() { int newCap = (int)(arra.length*GROWTH_RATE); T newA[] = (T[])new Object[newCap]; for (int i=0; icount) throw new ArrayIndexOutOfBoundsException(); if (index<0) throw new ArrayIndexOutOfBoundsException(); count++; if (count>=arra.length) grow(); for (int i=(count-1); i>=index; i--) { arra[i]=arra[i-1]; } arra[index]=t; } @Override public T get(int index) throws ArrayIndexOutOfBoundsException { if (index>count) throw new ArrayIndexOutOfBoundsException(); if (index<0) throw new ArrayIndexOutOfBoundsException(); return arra[index]; } @Override public void remove(int index) throws ArrayIndexOutOfBoundsException { if (index>count) throw new ArrayIndexOutOfBoundsException(); if (index<0) throw new ArrayIndexOutOfBoundsException(); for (int i=index; i<(count-1); i++) { arra[i]=arra[i+1]; } count--; } @Override public T set(int index, T t) throws ArrayIndexOutOfBoundsException { if (index>count) throw new ArrayIndexOutOfBoundsException(); if (index<0) throw new ArrayIndexOutOfBoundsException(); T oldValue = arra[index]; arra[index]=t; return oldValue; } @Override public int size() { return count; } @Override public int indexOf(T t) { for (int i=0; i=0; i--) { if (arra[i]==t) return i; } return -1; } @Override public void clear() { for (int i=0; i test = new GTList<>(); 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 = new Integer(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); } }