package class1_3_ArrayList_021; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class ArrayList implements List { private E[] array = (E[]) new Object[0]; /** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return true (as specified by {@link java.util.Collection#add}) */ @Override public boolean add(E e) { E[] newArray = (E[]) new Object[array.length+1]; for(int i = 0; i size()) */ @Override public void add(int index, E e) throws IndexOutOfBoundsException { //Counts and makes space for new element in the array for (int i = 0; i > array.length; i--) { array[i] = array[i - 1]; } array[index] = e; } /** * Removes all of the elements from this list. The list will * be empty after this call returns. */ @Override public void clear() { array = (E[]) new Object[0]; } /** * Returns true if this list contains the specified element. * More formally, returns true if and only if this list contains * at least one element e such that * (o==null ? e==null : o.equals(e)). * * @param o element whose presence in this list is to be tested * @return true if this list contains the specified element */ @Override public boolean contains(Object o) { if(o == null){ for(E element : array){ if(element == null){ return true; } } } else{ for(E element : array){ if(o.equals(element)){ return true; } } } return false; } /** * Returns the element at the specified position in this list. * * @param index index of the element to return * @return the element at the specified position in this list * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E get(int index) { throw new UnsupportedOperationException("This method isn't written yet."); } /** * 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 o element to search for * @return the index of the first occurrence of the specified element in * this list, or -1 if this list does not contain the element */ @Override public int indexOf(Object o){ for(int i = 0; itrue if this list contains no elements. * * @return true if this list contains no elements */ @Override public boolean isEmpty() // public boolean isEmpty(E e) { throw new UnsupportedOperationException("Does not compile"); // if(e.size() == 0) // { // return true; // } // else // { // return false; // } } /** * 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 * @return the element that was removed from the list * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E remove(int index) { if (index < 0 || index >= array.length) { throw new IndexOutOfBoundsException(); } E[] newArray = (E[]) new Object[array.length-1]; E holds; holds = array[index]; for (int i = 0; i < newArray.length; i++) { if (i >= index) { newArray[i] = array[i+1]; } else newArray[i] = array[i]; } array = newArray; return holds; } /** * Removes the first occurrence of the specified element from this list, * if it is present. If the list does not contain the element, it is * unchanged. More formally, removes the element with the lowest index * i such that * (o==null ? get(i)==null : o.equals(get(i))) * (if such an element exists). Returns true if this list * contained the specified element (or equivalently, if this list * changed as a result of the call). * * @param o element to be removed from this list, if present * @return true if this list contained the specified element */ @Override public boolean remove(Object o) { //flag for if the object was removed boolean removedObject = false; //the removedObjectIndes int removeIndex = -1; //linear search through array for(int i = 0; i < array.length; i++) { if(array[i] != null && array[i].equals(o)) { removeIndex = i; break; } } //check to see if object was found in array if(removeIndex >= 0){ //new array for storing after the remove operation E[] newArray = (E[]) new Object[array.length - 1]; //stores index for the new array int newIndex = 0; //copy for(int index = 0; index < array.length; index++) { if(index == removeIndex){ // Edit here removedObject = true; } else{ newArray[newIndex] = array[index]; newIndex++; } } //assign array = newArray; } //return the removed flag return removedObject; } /** * Replaces the element at the specified position in this list with the * specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= size()) */ @Override public E set(int index, E element) { throw new UnsupportedOperationException("This method isn't written yet."); } /** * Returns the number of elements in this list. * * @return the number of elements in this list */ @Override public int size() { throw new UnsupportedOperationException("Does not compile"); // for (int i = 0; i < array.length; i++){ // size++; // } // return size; } /** * Returns an array containing all of the elements in this list * in proper sequence (from first to last element). *

*

The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. *

*

This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this list in * proper sequence */ @Override public Object[] toArray() { throw new UnsupportedOperationException("This method isn't written yet."); } //-------- METHODS BEYOND THIS POINT ARE NOT IMPLEMENTED OR ALIGNED ------------ @Override public Iterator iterator() { return null; } @Override public T[] toArray(T[] a) { return null; } @Override public boolean containsAll(Collection c) { return false; } @Override public boolean addAll(Collection c) { return false; } @Override public boolean addAll(int index, Collection c) { return false; } @Override public boolean removeAll(Collection c) { return false; } @Override public boolean retainAll(Collection c) { return false; } @Override public int lastIndexOf(Object o) { return 0; } @Override public ListIterator listIterator() { return null; } @Override public ListIterator listIterator(int index) { return null; } @Override public List subList(int fromIndex, int toIndex) { return null; } }