package class3_1_ArrayListIterator_051; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.ListIterator; public class ArrayList implements List { /** * Returns an iterator over the elements in this list in proper sequence. * * @return an iterator over the elements in this list in proper sequence */ @Override public Iterator iterator() { // body of a method // System.out.println("this: "+this); return new Iterator() { // body of a class // // //public static int MY_CONSTANT =5; // public int myMember = 4; // /*package*/ int myMember2 = 4; // still instance variable // // class DontKnowWhy { // // } // //// for(int i = 0; i < 100; i++) { //// System.out.println("This is fun!"); //// } //// System.out.println("This is fun!");/ //// myMember = 5; int nextIndex = 0; /** * Returns {@code true} if the iteration has more elements. * (In other words, returns {@code true} if {@link #next} would * return an element rather than throwing an exception.) * * @return {@code true} if the iteration has more elements */ @Override public boolean hasNext() { // body of a method return nextIndex < array.length; } /** * Returns the next element in the iteration. * * @return the next element in the iteration * \@throws NoSuchElementException if the iteration has no more elements */ @Override public E next() { // System.out.println(ArrayList.this); // // E result = ArrayList.this.array[nextIndex]; // nextIndex++; // return result; nextIndex++; return array[nextIndex-1]; } }; } //============================= // Below here: Old stuff //============================= @SuppressWarnings("unchecked") public 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) { @SuppressWarnings("unchecked") E[] newArray = (E[]) new Object[array.length+1]; for(int i = 0; i < array.length; i++) { newArray[i] = array[i]; } newArray[array.length] = e; array = newArray; return true; } /** * 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 index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ @Override public void add(int index, E element) { @SuppressWarnings("unchecked") E[] newArray = (E[]) new Object[array.length + 1]; for (int i = 0; i < index; i++) newArray[i] = array[i]; for (int i = index + 1; i < array.length + 1; i++) newArray[i] = array[i - 1]; newArray[index] = element; array = newArray; } /** * Removes all of the elements from this list. The list will * be empty after this call returns. */ @Override public void clear() { for (E e : array) { this.remove(e); } } /** * 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){ @SuppressWarnings("unchecked") E[] newArray = (E[]) new Object[array.length]; boolean contains = false; for(int i = 0; i < array.length; i++){ if(array[i]==null) { if(o == null) { contains = true; } } else if(array[i].equals(o)){ contains = true; } } return contains; } /** * 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 {@inheritDoc} */ @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 */ /** * 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 * @throws ClassCastException if the type of the specified element * is incompatible with this list * (optional) * @throws NullPointerException if the specified element is null and this * list does not permit null elements * (optional) */ // @Override public int indexOf(Object o) { throw new UnsupportedOperationException("Compile error."); } // @Override // public int indexOf(E e){ // // int index = -1; // boolean found = false; // // for(int i = 0; (i < this.size()) && (!found); i++){ // try{ // // if(array[i].equals(e)){ // index = i; // found = true; // } // } // catch(NullPointerException npe){ // if(array[i] == e){ // index = i; // found = true; // } // } // } // return index; // } /** * Returns true if this list contains no elements. * * @return true if this list contains no elements */ @Override public boolean isEmpty() { return array.length == 0; } /** * 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 {@inheritDoc} */ @Override public E remove(int index) { throw new UnsupportedOperationException("This method isn't written yet."); } /** * 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) { boolean b = false;//value used in the while loop and returned if(array.length > 0) { E[] newArray = (E[]) new Object[array.length - 1];//array to be used if we remove anything int i = 0; int indexOfObject = -1; while (!b && i < array.length) {//checks if the object has been found already, iterating int doesn't exceed length if (array[i].equals(o)) {//check each element b = true; indexOfObject = i; } i++; } if (b) {//after the while-loop, checks if object was removed for (int a = 0; a < indexOfObject; a++) { newArray[a] = array[a];// first part of new array } for (int a = indexOfObject + 1; a < array.length; a++) { newArray[a - 1] = array[a];//second part of new array, avoids index of object to be removed } array = newArray;//points our array to the new array } } else { b = false; // element not found - no elements } return b; } /** * 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 {@inheritDoc} */ @Override public E set(int index, E element) { if(array.length > index){ array[index] = element; System.out.println("Set element at index "+index+" to "+element); return array[index]; } System.out.println("Index "+index+" does not exist in this array"); return null; } /** * Returns the number of elements in this list. * * @return the number of elements in this list */ @Override public int size() { throw new UnsupportedOperationException("This method isn't written yet."); } /** * 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. * * NOTE: THIS IMPLEMENTATION IS FOR A DIFFERENT VERSION THAN THE COMMENT. * THE VERSION IMPLEMENTED IS NICE BECAUSE IT ALLOWS CREATING AN ARRAY OF THE DESIRED TYPE. * GOOGLE Java 8 ArrayList toArray FOR DETAILS * * @return an array containing all of the elements in this list in * proper sequence */ @Override public T[] toArray(T[] a) { throw new UnsupportedOperationException("Compiler errors"); // T[] a = new T[array.length]; // for(E : array){ // a.add((T) E); // } // return a; } //--- Nothing below here is implemented /** * rETURNS A copy (not just a reference to the underlying array) * @return */ @Override public Object[] toArray() { Object[] result = new Object[array.length]; for(int i = 0; i< array.length; i++) { result[i] = array[i]; } return result; } @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; } }