diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..eace72f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 3e59c38..f21865a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,43 @@ +import edu.greenriver.sdev333.*; + +import java.util.Iterator; + public class Main { public static void main(String[] args) { + System.out.println("Hello world!"); + + List friends = new ArrayList(); + System.out.println("initial size is " + friends.size()); + + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + friends.add("Blanche"); + friends.add("Dorothy"); + friends.add("Sophia"); + System.out.println("size is now " + friends.size()); + + for (int i = 0; i < friends.size() ; i++) { + System.out.println(friends.get(i)); + } + + Iterator itr = friends.iterator(); + while (itr.hasNext()) { + String name = itr.next(); + System.out.println(name); + } + + for (String name : friends) { + System.out.println(name); + } + + + // friends.addAll(..) will } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..da46d83 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,389 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List { + + // WE NEED FIELDS!! + + // one plain old Java array + private ItemType[] data; + + // one int to keep track of size + // size is the # of spots that are used in the data array + // size is DIFFERENT than length + private int size; + + + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10]; + } + + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + int i = indexOf(item); + if (i != -1) { + return true; + } + + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return new OurCustomIterator(); + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) { + + // all of above code works until I run out of room + // when size becomes the same as length, I'm out of room + if (size == data.length) { + // resize up (double up the array size) + + // Step 1 - create a new larger array + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // Step 2 - copy items from data to temp + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + // Step 3 - repoint/refererence data to point to new array + data = temp; + + // Optional: + temp = null; + } // end of if (need to resize) + + data[size] = item; + size++; + } // end of method + + /** + * Removes a single instance of the specified item from this collection, + * if it is present. + * + * @param item item to be removed from this collection, if present + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void remove(ItemType item) { + int i = indexOf(item); + if (i != -1) { + // if it's found, use the other remove method to do the work + remove(i); + } + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + // lazy deletion + size = 0; + } + + /** + * Returns true if this collection contains all the items + * in the specified other collection. + * + * @param otherCollection collection to be checked for containment in this collection + * @return true if this collection contains all the items + * in the specified other collection + */ + @Override + public boolean containsAll(Collection otherCollection) { + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)) { + return false; + } + } + + return true; + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + throw new UnsupportedOperationException("Not gonna do it!"); + } + + /** + * Removes all of this collection's items that are also contained in the + * specified other collection. After this call returns, this collection will + * contain no elements in common with the specified other collection. + * + * @param otherCollection collection containing elements to be removed + * from this collection + */ + @Override + public void removeAll(Collection otherCollection) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if (index >= size) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; + } + + /** + * Replaces the item at the specified position in this list + * with the specified item + * + * @param index index of the item to replace + * @param item item to be stored at the specified position + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void set(int index, ItemType item) { + if (index >= size) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + data[index] = item; + } + + /** + * Inserts the specified item at the specified position in this list. + * Shifts the item currently at that position (if any) and any subsequent + * items to the right. + * + * @param index index at which the specified item is to be inserted + * @param item item to be inserted + * @throws NullPointerException if the specified item is null + * and this list does not permit null elements + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void add(int index, ItemType item) { + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + } + + /** + * Removes the element at the specified position in this list. + * Shifts any subsequent items to the left. + * + * @param index the index of the item to be removed + * @throws IndexOutOfBoundsException if the index is out of range + * (index < 0 || index >= size()) + */ + @Override + public void remove(int index) { + for (int i = index; i < size - 1; i++) { + data[i] = data[i + 1]; + } + size--; + } + + /** + * Returns the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int indexOf(ItemType item) { + for (int i = 0; i < size; i++) { + if (item.equals(data[i])) { + return i; + } + } + // if we got here it was not in the array + return -1; + } + + /** + * Returns the index of the last occurrence of the specified item + * in this list, or -1 if this list does not contain the item. + * + * @param item the item to search for + * @return the index of the first occurrence of the specified item + * in this list, or -1 if this list does not contain the item + * @throws NullPointerException if the specified item is null and this + * list does not permit null items + */ + @Override + public int lastIndexOf(ItemType item) { + return 0; + } + + /** + * Returns a list iterator over the elements in this list + * (in proper sequence). + * + * @return a list iterator over the elements in this list + * (in proper sequence) + */ + @Override + public ListIterator listIterator() { + return null; + } + + public class OurCustomIterator implements Iterator{ + // fields + private int currentPosition; + + public OurCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + } + + public class SecondCustomIterator implements ListIterator { + // fancier Iterator - lets us go forwards and backwards + private int currentPosition; + + public SecondCustomIterator() { + currentPosition = 0; + } + + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + + @Override + public boolean hasPrevious() { + return currentPosition > 0; + } + + @Override + public ItemType previous() { + ItemType result = get(currentPosition); + currentPosition--; + return result; + } + + @Override + public int nextIndex() { + if (currentPosition < size()) + return size() + 1; + else + return 0; + } + + @Override + public int previousIndex() { + return 0; + } + + @Override + public void remove() { + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } +}