From 2bba85f651dd69a8934c25ed213d8f098052d331 Mon Sep 17 00:00:00 2001 From: dknoll1 Date: Wed, 11 Jan 2023 14:36:48 -0800 Subject: [PATCH 1/2] example from class --- .idea/misc.xml | 2 +- .idea/vcs.xml | 6 + src/Main.java | 20 ++ src/edu/greenriver/sdev333/ArrayList.java | 273 ++++++++++++++++++++++ 4 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/ArrayList.java 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..de619d9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,25 @@ +import edu.greenriver.sdev333.*; + 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()); + } } \ 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..12d3196 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,273 @@ +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) { + 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 null; + } + + /** + * 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) { + + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + + } + + /** + * 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) { + return false; + } + + /** + * 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) { + + } + + /** + * 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) { + return null; + } + + /** + * 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) { + + } + + /** + * 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) { + + } + + /** + * 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) { + + } + + /** + * 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) { + return 0; + } + + /** + * 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; + } +} From b3ac192e57216b011e6dc782a49d59a95b30de85 Mon Sep 17 00:00:00 2001 From: dknoll1 Date: Wed, 18 Jan 2023 14:49:51 -0800 Subject: [PATCH 2/2] added constructor, size, add, etc --- src/Main.java | 18 +++ src/edu/greenriver/sdev333/ArrayList.java | 136 ++++++++++++++++++++-- 2 files changed, 144 insertions(+), 10 deletions(-) diff --git a/src/Main.java b/src/Main.java index de619d9..f21865a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,7 @@ import edu.greenriver.sdev333.*; +import java.util.Iterator; + public class Main { public static void main(String[] args) { @@ -21,5 +23,21 @@ public static void main(String[] args) { 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 index 12d3196..da46d83 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -52,6 +52,11 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + int i = indexOf(item); + if (i != -1) { + return true; + } + return false; } @@ -62,7 +67,7 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + return new OurCustomIterator(); } /** @@ -109,7 +114,11 @@ public void add(ItemType item) { */ @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); + } } /** @@ -118,7 +127,8 @@ public void remove(ItemType item) { */ @Override public void clear() { - + // lazy deletion + size = 0; } /** @@ -131,7 +141,15 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { + ItemType itemToCheck = itr.next(); + if (!contains(itemToCheck)) { + return false; + } + } + + return true; } /** @@ -141,7 +159,7 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - + throw new UnsupportedOperationException("Not gonna do it!"); } /** @@ -180,7 +198,10 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if (index >= size) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; } /** @@ -196,7 +217,10 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + if (index >= size) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + data[index] = item; } /** @@ -213,7 +237,9 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } } /** @@ -226,7 +252,10 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { - + for (int i = index; i < size - 1; i++) { + data[i] = data[i + 1]; + } + size--; } /** @@ -241,7 +270,13 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + 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; } /** @@ -270,4 +305,85 @@ public int lastIndexOf(ItemType item) { 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) { + + } + } }