package class2_1_ImplementingArrayList; /* * Instructor notes: * Rearranged methods before class */ import java.util.*; public class ArrayList implements List { private T[] array; public ArrayList() { clear(); } @Override public int size() { return array.length; } @Override public boolean isEmpty() { return array.length == 0; } /** * Add t to end of array * @param t element to add * @return true if added */ @Override public boolean add(T t) { T[] temp = (T[]) new Object[array.length+1]; // Add to end of array temp[array.length] = t; for(int i=0; i= 0; i++) { // (o==null ? get(i)==null : o.equals(get(i))) if(o==null) { if(array[i]==null) { result = i; } } else { if(o.equals(array[i])) { result = i; } } } return result; } @Override public void clear() { array = (T[]) new Object[0]; } @Override public void add(int index, T element) { int size = 0; for(int count = 0; count < array.length; count ++) { if(array[count] != null) { size ++; } } if(size == array.length) { array = Arrays.copyOf(array, array.length + 1); } for(int count = 0; count < (array.length - size); count ++) { array[array.length - count - 1] = array[array.length - count - 1 - 1]; } array[index] = element; } @Override public boolean contains(Object o) { Object temp = 0; for(int i = 0; i < array.length; i++){ if(array[i] == temp){ return true; } else return false; } return false; } @Override public T get(int index) { if(index<0 || index>=size()){ throw new IndexOutOfBoundsException(); } return array[index]; } @Override public T set(int index, T element) { T replaced = array[index]; T[] temp = (T[]) new Object[array.length]; for (int i=0; i subList(int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > array.length) { throw new IndexOutOfBoundsException(); } else if(fromIndex > toIndex) { throw new IllegalArgumentException(); } T[] temp = (T[]) new Object[toIndex - fromIndex]; for(int i = fromIndex; i < toIndex; i++) { temp[i] = array[i]; } return Arrays.asList(temp); } @Override public Object[] toArray() { Object[] tempArray = new Object[size()]; for(int i=0;i iterator() { return null; //To change body of implemented methods use File | Settings | File Templates. } //========= UNIMPLEMENTED METHODS ================ @Override public T1[] toArray(T1[] a) { throw new UnsupportedOperationException(); } @Override public int lastIndexOf(Object o) { throw new UnsupportedOperationException(); } @Override public ListIterator listIterator() { throw new UnsupportedOperationException(); } @Override public ListIterator listIterator(int index) { throw new UnsupportedOperationException(); } @Override public boolean containsAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean addAll(int index, Collection c) { throw new UnsupportedOperationException(); } @Override public boolean removeAll(Collection c) { throw new UnsupportedOperationException(); } @Override public boolean retainAll(Collection c) { return false; //To change body of implemented methods use File | Settings | File Templates. } }