From ea22c9ec0488763d985b0d6cb6a85829978d9011 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:21:59 -0800 Subject: [PATCH 01/20] ArrayList class --- .idea/vcs.xml | 6 ++++++ src/edu/greenriver/sdev333/ArrayList.java | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 .idea/vcs.xml create mode 100644 src/edu/greenriver/sdev333/ArrayList.java 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/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..0bbafe8 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,4 @@ +package edu.greenriver.sdev333; + +public class ArrayList { +} From 0d6446bc5d50010725cd233b07f36ddf9671a10c Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:26:56 -0800 Subject: [PATCH 02/20] ok --- src/edu/greenriver/sdev333/ArrayList.java | 232 +++++++++++++++++++++- 1 file changed, 231 insertions(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 0bbafe8..5945ab8 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -1,4 +1,234 @@ package edu.greenriver.sdev333; -public class ArrayList { +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List{ + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return 0; + } + + /** + * 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) { + + } + + /** + * 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 36a00991192cbff42d22d873987480477995b6f5 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:46:03 -0800 Subject: [PATCH 03/20] constructor --- src/edu/greenriver/sdev333/ArrayList.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 5945ab8..2155464 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -4,6 +4,18 @@ import java.util.ListIterator; public class ArrayList implements List{ +//WE NEED FIELDS + + //one java array + private ItemType[] data; + + //size (# of spots being used by data array) + private int size; + + public ArrayList() { + data = (ItemType[]) new Object[10]; + size = 0; + } /** * Returns the number of items in this collection. From 801df9ac60f9019f789f607d7a7e0010e53ba7e5 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:06:31 -0800 Subject: [PATCH 04/20] store --- src/Main.java | 8 ++++++++ src/edu/greenriver/sdev333/ArrayList.java | 8 +++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Main.java b/src/Main.java index 3e59c38..4355caa 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,13 @@ +import edu.greenriver.sdev333.ArrayList; +import edu.greenriver.sdev333.List; + 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"); + 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 index 2155464..19be09c 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -24,7 +24,7 @@ public ArrayList() { */ @Override public int size() { - return 0; + return size; } /** @@ -69,7 +69,13 @@ public Iterator iterator() { */ @Override public void add(ItemType item) { + //check that there is room to add, if not resize array + if(size == data.length){ + }else { + data[size] = item; + size++; + } } /** From f0611c73c0a5db61aa86acd62acdbba870301931 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:21:48 -0800 Subject: [PATCH 05/20] add --- src/edu/greenriver/sdev333/ArrayList.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 19be09c..fe9065b 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -71,11 +71,23 @@ public Iterator iterator() { public void add(ItemType item) { //check that there is room to add, if not resize array if(size == data.length){ + //create larger array + ItemType[] temp = (ItemType[]) new Object[(size * 2)]; - }else { - data[size] = item; - size++; + //copy data items to temp + for(int i =0; i < size; i++){ + temp [i] = data[i]; + } + + //repoint data to temp + data = temp; + + //optional +// temp = null; } + data[size] = item; + size++; + } /** From f18a2547fe2e238b68ab87c3bb4a5dafffd39dbe Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:48:24 -0800 Subject: [PATCH 06/20] ArrayList methods --- src/Main.java | 14 ++++++++++++ src/edu/greenriver/sdev333/ArrayList.java | 26 ++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 4355caa..03b33f8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -9,5 +9,19 @@ public static void main(String[] args) { System.out.println("Initial size is: " + friends.size()); friends.add("Jess"); System.out.println("Size is now: " + friends.size()); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + friends.add("Jess"); + 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 index fe9065b..c294b70 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -34,7 +34,7 @@ public int size() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -47,6 +47,12 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + //do this + for(int i = 0; i otherCollection) { - return false; +// return false; + throw new UnsupportedOperationException("method not implemented"); } /** @@ -171,7 +179,11 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if(index >= size){ + throw new ArrayIndexOutOfBoundsException(); + }else { + return data[index]; + } } /** @@ -187,7 +199,11 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + if(index > size){ + throw new ArrayIndexOutOfBoundsException(); + }else { + data[index] = item; + } } /** From ffabfe38313613c79a479dbad0797430342cab14 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 18 Jan 2023 13:34:04 -0800 Subject: [PATCH 07/20] added iterator --- src/Main.java | 36 ++++++++++++------- src/edu/greenriver/sdev333/ArrayList.java | 43 +++++++++++++++++++++-- 2 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/Main.java b/src/Main.java index 03b33f8..6d80256 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,8 @@ import edu.greenriver.sdev333.ArrayList; import edu.greenriver.sdev333.List; +import java.util.Iterator; + public class Main { public static void main(String[] args) { @@ -9,19 +11,29 @@ public static void main(String[] args) { System.out.println("Initial size is: " + friends.size()); friends.add("Jess"); System.out.println("Size is now: " + friends.size()); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); - friends.add("Jess"); + friends.add("John"); + friends.add("Jake"); + friends.add("Jenny"); + friends.add("Joey"); + friends.add("Jordan"); + friends.add("Justin"); + friends.add("Jill"); + friends.add("Joe"); + friends.add("Johnny"); + friends.add("Jacobi"); + friends.add("Juliet"); + friends.add("Jeremy"); System.out.println("Size is now: " + friends.size()); + Iterator itr = friends.iterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + +// for(int i=0; i< friends.size(); i++){ +// System.out.println(friends.get(i)); +// } + } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index c294b70..e31c2ed 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -2,6 +2,7 @@ import java.util.Iterator; import java.util.ListIterator; +import java.util.function.Consumer; public class ArrayList implements List{ //WE NEED FIELDS @@ -63,7 +64,7 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + return new OurIterator(); } /** @@ -248,7 +249,12 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + for (int i = 0; i < size; i++) { + if (data[i].equals(item)) { + return i; + } + } + return -1; } /** @@ -277,4 +283,35 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } -} + + private class OurIterator implements Iterator{ + private int current; + + public OurIterator() { + current = 0; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public ItemType next() { + ItemType next = get(current); + current++; + return next; + + } + + @Override + public void remove() { + Iterator.super.remove(); + } + + @Override + public void forEachRemaining(Consumer action) { + Iterator.super.forEachRemaining(action); + } + } +}//end of ArrayList From cfe299a1afd2ed53bcecfe693db9f0c765c36e91 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:36:37 -0800 Subject: [PATCH 08/20] finished ArrayList methods --- src/Main.java | 16 ++- src/edu/greenriver/sdev333/ArrayList.java | 133 ++++++++++++++++++---- 2 files changed, 124 insertions(+), 25 deletions(-) diff --git a/src/Main.java b/src/Main.java index 6d80256..7491ddd 100644 --- a/src/Main.java +++ b/src/Main.java @@ -25,12 +25,26 @@ public static void main(String[] args) { friends.add("Jeremy"); System.out.println("Size is now: " + friends.size()); + friends.add(1, "Hello"); + friends.remove("Hello"); + + List friends2 = new ArrayList(); + friends2.add("John"); + friends2.add("Jak"); + friends2.add("Jenny"); + friends2.add("Joey"); + friends2.add("Jordan"); + friends2.add("Justin"); + friends2.add("Jill"); + friends2.add("Joe"); + Iterator itr = friends.iterator(); while(itr.hasNext()){ String name = itr.next(); System.out.println(name); } - + System.out.println("Size is now: " + friends.size()); + System.out.println(friends.containsAll(friends2)); // for(int i=0; i< friends.size(); i++){ // System.out.println(friends.get(i)); // } diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index e31c2ed..4b496a4 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -48,11 +48,8 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - //do this - for(int i = 0; i iterator() { return new OurIterator(); } - - /** - * 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) { - //check that there is room to add, if not resize array + private void checkSize(){ if(size == data.length){ //create larger array ItemType[] temp = (ItemType[]) new Object[(size * 2)]; @@ -89,9 +76,19 @@ public void add(ItemType item) { //repoint data to temp data = temp; - //optional -// temp = 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) { + checkSize(); data[size] = item; size++; @@ -108,7 +105,10 @@ public void add(ItemType item) { //do this one @Override public void remove(ItemType item) { - + int index = indexOf(item); + if(index != -1) { + remove(index); + } } /** @@ -130,8 +130,14 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { -// return false; - throw new UnsupportedOperationException("method not implemented"); + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + ItemType cur = itr.next(); + if(!contains(cur)){ + return false; + } + } + return true; } /** @@ -221,7 +227,12 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + checkSize(); + for (int i = size; i >= index + 1; i--) { + data[i] = data[i - 1]; + } + data[index] = item; + size++; } /** @@ -234,7 +245,11 @@ 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]; + } + data[size - 1] = null; + size--; } /** @@ -269,7 +284,12 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + for (int i = size; i >= 0; i++) { + if (data[i].equals(item)) { + return i; + } + } + return -1; } /** @@ -314,4 +334,69 @@ public void forEachRemaining(Consumer action) { Iterator.super.forEachRemaining(action); } } + + private class SecondOurIterator implements ListIterator{ + int current; + + public SecondOurIterator() { + current = 0; + } + + @Override + public boolean hasNext() { + return current < size; + } + + @Override + public ItemType next() { + ItemType next = get(current); + current++; + return next; + } + + @Override + public boolean hasPrevious() { + return current > 0; + } + + @Override + public ItemType previous() { + if(hasPrevious()){ + ItemType a = get(current--); + return a; + } + return null; + } + + @Override + public int nextIndex() { + if(hasNext()) { + return current++; + } + return -1; + } + + @Override + public int previousIndex() { + if(hasPrevious()){ + return current--; + } + return -1; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } }//end of ArrayList From b70bf8dadf6d0b55559bec0c7f79b3680a032cd9 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:52:59 -0800 Subject: [PATCH 09/20] added isValidIndex method --- src/edu/greenriver/sdev333/ArrayList.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 4b496a4..a1b398a 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -17,7 +17,11 @@ public ArrayList() { data = (ItemType[]) new Object[10]; size = 0; } + public ArrayList(int size) { + this.size = size; + data = (ItemType[]) new Object[size]; + } /** * Returns the number of items in this collection. * @@ -147,7 +151,10 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - + for (ItemType item : + otherCollection) { + add(item); + } } /** @@ -160,7 +167,10 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - + for (ItemType item : + otherCollection) { + remove(item); + } } /** @@ -175,7 +185,9 @@ public void removeAll(Collection otherCollection) { public void retainAll(Collection otherCollection) { } - + public boolean isValidIndex(int index){ + return index < size; + } /** * Returns the item at the specified position in this list * @@ -186,7 +198,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if(index >= size){ + if(!isValidIndex(index)){ throw new ArrayIndexOutOfBoundsException(); }else { return data[index]; @@ -206,7 +218,7 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - if(index > size){ + if(!isValidIndex(index)){ throw new ArrayIndexOutOfBoundsException(); }else { data[index] = item; From 74f79d12eedf31640ae00337d1f17496d4ed7284 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Thu, 19 Jan 2023 17:50:59 -0800 Subject: [PATCH 10/20] Finished building ArrayList Methods and helper methods --- ImplementingLists.iml | 1 + src/Main.java | 62 +++++++++++- src/edu/greenriver/sdev333/ArrayList.java | 118 ++++++++++++++++++---- 3 files changed, 156 insertions(+), 25 deletions(-) diff --git a/ImplementingLists.iml b/ImplementingLists.iml index c90834f..e5d2daf 100644 --- a/ImplementingLists.iml +++ b/ImplementingLists.iml @@ -4,6 +4,7 @@ + diff --git a/src/Main.java b/src/Main.java index 7491ddd..0f835a4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -25,8 +25,16 @@ public static void main(String[] args) { friends.add("Jeremy"); System.out.println("Size is now: " + friends.size()); + //test add at index friends.add(1, "Hello"); + //test add with errors +// friends.add(-1, "Hello"); + + //test remove by item friends.remove("Hello"); + //test remove by index + friends.remove(0); + List friends2 = new ArrayList(); friends2.add("John"); @@ -38,16 +46,62 @@ public static void main(String[] args) { friends2.add("Jill"); friends2.add("Joe"); + List friends3 = new ArrayList(); + friends3.add("Jose"); + friends3.add("Jun"); + friends3.add("Jeremiah"); + + + //test iterator Iterator itr = friends.iterator(); while(itr.hasNext()){ String name = itr.next(); System.out.println(name); } - System.out.println("Size is now: " + friends.size()); + + //test isEmpty method + System.out.println("Is this array empty: " + friends.isEmpty()); + + //test contains method + System.out.println("This array contains John: " + friends.contains("John")); + System.out.println("This array contains Joh: " + friends.contains("Joh")); +// System.out.println("This method catches null parameter: " + friends.contains(null)); + + //test containsAll System.out.println(friends.containsAll(friends2)); -// for(int i=0; i< friends.size(); i++){ -// System.out.println(friends.get(i)); -// } + + //test retainsAll + friends.retainAll(friends2); + for(int i=0; i< friends.size(); i++){ + System.out.println(friends.get(i)); + } + + //test size method + System.out.println("Size is now: " + friends.size()); + System.out.println(); + + //test addAll + friends.addAll(friends3); + for(int i=0; i< friends.size(); i++){ + System.out.println(friends.get(i)); + } + System.out.println(); + + //test removeAll + friends.removeAll(friends3); + for(int i=0; i< friends.size(); i++){ + System.out.println(friends.get(i)); + } + System.out.println(); + + //test set + friends.set(0, "Jesus"); + for(int i=0; i< friends.size(); i++){ + System.out.println(friends.get(i)); + } + + //test indexOf + System.out.println("Index of Jesus: " + friends.indexOf("Jesus")); } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index a1b398a..65e8362 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -4,6 +4,14 @@ import java.util.ListIterator; import java.util.function.Consumer; +/** + * @author Katherine Watkins + * SDEV 333 + * 01/09/2023 + * Building ArrayList Class + * @param = type stored in ArrayList + */ + public class ArrayList implements List{ //WE NEED FIELDS @@ -13,18 +21,73 @@ public class ArrayList implements List{ //size (# of spots being used by data array) private int size; + /** + * constructor with no variables passed in for length + */ public ArrayList() { data = (ItemType[]) new Object[10]; size = 0; } + + /** + * constructor + * @param size = length of ArrayList to be made + */ public ArrayList(int size) { this.size = size; data = (ItemType[]) new Object[size]; } + + /** + * HELPER METHODS + */ + + + /** + * helper method to check if array is at max capacity, + * if so it will double the array length + */ + private void checkSize(){ + if(size == data.length){ + //create larger array + ItemType[] temp = (ItemType[]) new Object[(size * 2)]; + + //copy data items to temp + for(int i =0; i < size; i++){ + temp [i] = data[i]; + } + + //repoint data to temp + data = temp; + + } + } + + /** + * helper method to check if item passed into methods is null + * @param item item to be checked if null + */ + private boolean isNull(ItemType item){ + if(item == null){ + return true; + } + return false; + } + + /** + * helper method to check if index is valid + * @param index index to be checked + * @return returns true if index is within array limits + */ + + public boolean isValidIndex(int index){ + return (index < size && index >= 0); + } + + + /** - * Returns the number of items in this collection. - * * @return the number of items in this collection */ @Override @@ -33,8 +96,6 @@ public int size() { } /** - * Returns true if this collection contains no items. - * * @return true if this collection contains no items */ @Override @@ -52,6 +113,9 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + if(isNull(item)){ + throw new NullPointerException(); + }; if(indexOf(item) != -1){ return true; } @@ -67,21 +131,8 @@ public boolean contains(ItemType item) { public Iterator iterator() { return new OurIterator(); } - private void checkSize(){ - if(size == data.length){ - //create larger array - ItemType[] temp = (ItemType[]) new Object[(size * 2)]; - //copy data items to temp - for(int i =0; i < size; i++){ - temp [i] = data[i]; - } - - //repoint data to temp - data = temp; - } - } /** * Adds the specified item to the collection. * @@ -109,6 +160,9 @@ public void add(ItemType item) { //do this one @Override public void remove(ItemType item) { + if(isNull(item)){ + throw new NullPointerException(); + }; int index = indexOf(item); if(index != -1) { remove(index); @@ -181,13 +235,23 @@ public void removeAll(Collection otherCollection) { * @param otherCollection collection containing elements to be retained in * this collection */ + @Override public void retainAll(Collection otherCollection) { - - } - public boolean isValidIndex(int index){ - return index < size; + ItemType[] temp = (ItemType[]) new Object[10]; + int tempSize = 0; + for (ItemType item : + otherCollection) { + if(contains(item)) { + temp[tempSize] = item; + tempSize++; + } + } + data = temp; + size = tempSize; } + + /** * Returns the item at the specified position in this list * @@ -218,6 +282,9 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + if(isNull(item)){ + throw new NullPointerException(); + }; if(!isValidIndex(index)){ throw new ArrayIndexOutOfBoundsException(); }else { @@ -239,6 +306,12 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { + if(!isValidIndex(index)){ + throw new ArrayIndexOutOfBoundsException(); + } + if(isNull(item)){ + throw new NullPointerException(); + }; checkSize(); for (int i = size; i >= index + 1; i--) { data[i] = data[i - 1]; @@ -257,6 +330,9 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + if(!isValidIndex(index)){ + throw new ArrayIndexOutOfBoundsException(); + } for (int i = index; i < size-1; i++){ data[i] = data[i+1]; } From cbcd21e66f5cfdfe8938ffa1750d096cb8d9c3c0 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 23 Jan 2023 14:42:26 -0800 Subject: [PATCH 11/20] started LinkedList class --- .../greenriver/sdev333/SinglyLinkedList.java | 251 ++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..ce3239a --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,251 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class SinglyLinkedList implements List{ + //Fields- What is in a LinkedList? + private int size; + private Node head; + + + //helper/inner class + private class Node{ + ItemType data; + Node next; + } + + //constructor + + public SinglyLinkedList(){ + head = null; + size = 0; + } + /** + * 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 size == 0; + } + + /** + * 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) { + + } + + /** + * 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() { + head = null; + 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) { + 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 e143bff286d1fa00fb71df811dffb8daa5b604e5 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 25 Jan 2023 14:46:44 -0800 Subject: [PATCH 12/20] add method in LinkedList --- .../greenriver/sdev333/SinglyLinkedList.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index ce3239a..aea648c 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -13,6 +13,7 @@ public class SinglyLinkedList implements List{ private class Node{ ItemType data; Node next; + } //constructor @@ -73,7 +74,25 @@ public Iterator iterator() { */ @Override public void add(ItemType item) { - + //create new node for item, catch null pointer + Node add = new Node(); + if(item == null){ + throw new NullPointerException(); + }else{ + add.data = item; + add.next = null; + } + //add to end of LinkedList + if(head == null){ + head = add; + }else{ + Node current = head; + while(current.next != null){ + current = current.next; + } + current.next = add; + } + size++; } /** From d3c5d01adf353684d4eaeeed963019aefdd99c60 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 30 Jan 2023 13:05:47 -0800 Subject: [PATCH 13/20] commit changes to linked list --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index ce3239a..b28c17a 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -13,6 +13,11 @@ public class SinglyLinkedList implements List{ private class Node{ ItemType data; Node next; + + public Node(ItemType data, Node next) { + this.data = data; + this.next = next; + } } //constructor From cb926f78e139320f166065e7cb3643e59a298ed0 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 30 Jan 2023 13:12:03 -0800 Subject: [PATCH 14/20] get method --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index aea648c..84bb4c9 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -177,7 +177,14 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + Node current = head; + int counter = 0; + while (counter != index){ + current = current.next; + counter++; + } + + return current.data; } /** From 2dc3b9197c4b2e1195541c113234d7fbb2150fd7 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 30 Jan 2023 14:11:40 -0800 Subject: [PATCH 15/20] add methods added --- .../greenriver/sdev333/SinglyLinkedList.java | 263 +++++++++++++++++- 1 file changed, 261 insertions(+), 2 deletions(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 84bb4c9..66563cf 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -22,6 +22,52 @@ public SinglyLinkedList(){ head = null; size = 0; } + + //helper methods + public void validIndex(int index){ + if(index<0 || index>= size){ + throw new IndexOutOfBoundsException(); + } + } + + public class ourCustomIterator implements Iterator{ + + /** + * 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 + */ + + //field to keep track of position + private Node currentPosition; + + public ourCustomIterator() { + currentPosition = head; + } + + @Override + public boolean hasNext() { + //check if current.next == null + return currentPosition != null; + + } + + /** + * 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 ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + + return result; + } + } /** * Returns the number of items in this collection. * @@ -62,7 +108,8 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + //returns our iterator + return new ourCustomIterator(); } /** @@ -177,6 +224,9 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { + //check index validity + validIndex(index); + Node current = head; int counter = 0; while (counter != index){ @@ -200,6 +250,16 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + validIndex(index); + + Node current = head; + int counter = 0; + while (counter != index){ + current = current.next; + counter++; + } + current.data = item; + } @@ -217,6 +277,26 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { + validIndex(index); + Node temp = new Node(); + Node current = head; + int counter= 0; + if(index == 0){ + temp.data = item; + temp.next = head; + head = temp; + }else { + while (counter < index - 1) { + current = current.next; + counter++; + } + + temp.data = item; + temp.next = current.next; + + current.next = temp; + } + size++; } @@ -230,7 +310,20 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + validIndex(index); + if(index == 0){ + head = head.next; + }else { + Node current = head; + int counter = 0; + while (counter < index - 1) { + current = current.next; + counter++; + } + current.next = current.next.next; + } + size--; } /** @@ -272,6 +365,172 @@ public int lastIndexOf(ItemType item) { */ @Override public ListIterator listIterator() { - return null; + + return new OurEnhancedIterator(); + } + + private class OurEnhancedIterator implements ListIterator{ + + private Node currentPosition; + + public OurEnhancedIterator(){ + currentPosition = head; + } + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the forward direction. (In other words, + * returns {@code true} if {@link #next} would return an element rather + * than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the forward direction + */ + @Override + public boolean hasNext() { + return currentPosition != null; + } + + /** + * Returns the next element in the list and advances the cursor position. + * This method may be called repeatedly to iterate through the list, + * or intermixed with calls to {@link #previous} to go back and forth. + * (Note that alternating calls to {@code next} and {@code previous} + * will return the same element repeatedly.) + * + * @return the next element in the list + * @throws //NoSuchElementException if the iteration has no next element + */ + @Override + public ItemType next() { + return null; + } + + /** + * Returns {@code true} if this list iterator has more elements when + * traversing the list in the reverse direction. (In other words, + * returns {@code true} if {@link #previous} would return an element + * rather than throwing an exception.) + * + * @return {@code true} if the list iterator has more elements when + * traversing the list in the reverse direction + */ + @Override + public boolean hasPrevious() { + return false; + } + + /** + * Returns the previous element in the list and moves the cursor + * position backwards. This method may be called repeatedly to + * iterate through the list backwards, or intermixed with calls to + * {@link #next} to go back and forth. (Note that alternating calls + * to {@code next} and {@code previous} will return the same + * element repeatedly.) + * + * @return the previous element in the list + * @throws //NoSuchElementException if the iteration has no previous + * element + */ + @Override + public ItemType previous() { + return null; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #next}. (Returns list size if the list + * iterator is at the end of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code next}, or list size if the list + * iterator is at the end of the list + */ + @Override + public int nextIndex() { + return 0; + } + + /** + * Returns the index of the element that would be returned by a + * subsequent call to {@link #previous}. (Returns -1 if the list + * iterator is at the beginning of the list.) + * + * @return the index of the element that would be returned by a + * subsequent call to {@code previous}, or -1 if the list + * iterator is at the beginning of the list + */ + @Override + public int previousIndex() { + return 0; + } + + /** + * Removes from the list the last element that was returned by {@link + * #next} or {@link #previous} (optional operation). This call can + * only be made once per call to {@code next} or {@code previous}. + * It can be made only if {@link #add} has not been + * called after the last call to {@code next} or {@code previous}. + * + * @throws UnsupportedOperationException if the {@code remove} + * operation is not supported by this list iterator + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void remove() { + + } + + /** + * Replaces the last element returned by {@link #next} or + * {@link #previous} with the specified element (optional operation). + * This call can be made only if neither {@link #remove} nor {@link + * #add} have been called after the last call to {@code next} or + * {@code previous}. + * + * @param itemType the element with which to replace the last element returned by + * {@code next} or {@code previous} + * @throws UnsupportedOperationException if the {@code set} operation + * is not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of the specified + * element prevents it from being added to this list + * @throws IllegalStateException if neither {@code next} nor + * {@code previous} have been called, or {@code remove} or + * {@code add} have been called after the last call to + * {@code next} or {@code previous} + */ + @Override + public void set(ItemType itemType) { + + } + + /** + * Inserts the specified element into the list (optional operation). + * The element is inserted immediately before the element that + * would be returned by {@link #next}, if any, and after the element + * that would be returned by {@link #previous}, if any. (If the + * list contains no elements, the new element becomes the sole element + * on the list.) The new element is inserted before the implicit + * cursor: a subsequent call to {@code next} would be unaffected, and a + * subsequent call to {@code previous} would return the new element. + * (This call increases by one the value that would be returned by a + * call to {@code nextIndex} or {@code previousIndex}.) + * + * @param itemType the element to insert + * @throws UnsupportedOperationException if the {@code add} method is + * not supported by this list iterator + * @throws ClassCastException if the class of the specified element + * prevents it from being added to this list + * @throws IllegalArgumentException if some aspect of this element + * prevents it from being added to this list + */ + @Override + public void add(ItemType itemType) { + + } } } From 082847627ffaa598cf6fb943e73557a47d27ea7a Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 30 Jan 2023 14:48:05 -0800 Subject: [PATCH 16/20] added indexOf and lastIndex methods --- .../greenriver/sdev333/SinglyLinkedList.java | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 66563cf..b872f24 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -29,6 +29,11 @@ public void validIndex(int index){ throw new IndexOutOfBoundsException(); } } + public void isNull(ItemType item){ + if(item == null){ + throw new NullPointerException(); + } + } public class ourCustomIterator implements Iterator{ @@ -98,7 +103,8 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { - return false; + isNull(item); + return indexOf(item) != -1; } /** @@ -121,6 +127,7 @@ public Iterator iterator() { */ @Override public void add(ItemType item) { + isNull(item); //create new node for item, catch null pointer Node add = new Node(); if(item == null){ @@ -152,6 +159,23 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { + isNull(item); + //or use IndexOf and remove(index) + if(head.data == item){ + head = head.next; + }else{ + Node current = head; + Node previous; + while(current.next != null){ + previous = current; + current = current.next; + + if(current.data.equals(item)){ + previous.next = current.next; + size--; + } + } + } } @@ -338,7 +362,18 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + isNull(item); + + int counter = 0; + Node cur = head; + while(cur != null){ + if(cur.data.equals(item)){ + return counter; + } + counter++; + cur = cur.next; + } + return -1; } /** @@ -353,7 +388,18 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + isNull(item); + int lastIndex = -1; + int counter = 0; + Node cur = head; + while(cur != null){ + if(cur.data.equals(item)){ + lastIndex = counter; + } + counter++; + cur = cur.next; + } + return lastIndex; } /** From eca348cce9308d292664dc78be2bfcfcf680ded3 Mon Sep 17 00:00:00 2001 From: kat-coding <83139590+kat-coding@users.noreply.github.com> Date: Wed, 1 Feb 2023 14:43:16 -0800 Subject: [PATCH 17/20] addAll method --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index aea648c..e0a1035 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -138,7 +138,10 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - + for (ItemType item: otherCollection + ) { + this.add(item); + } } /** From 4775b3d2cc1db16db20ad7ff1029b51324a72a61 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Fri, 3 Feb 2023 18:00:00 -0800 Subject: [PATCH 18/20] finished required LinkedList methods --- src/Main.java | 44 +++++++++++-------- .../greenriver/sdev333/SinglyLinkedList.java | 39 +++++++++++++--- 2 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/Main.java b/src/Main.java index 0f835a4..4f82932 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ import edu.greenriver.sdev333.ArrayList; import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.SinglyLinkedList; import java.util.Iterator; @@ -7,10 +8,10 @@ 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()); + List friends = new SinglyLinkedList<>(); + System.out.println("Initial size is: " + friends.size()); //0 friends.add("Jess"); - System.out.println("Size is now: " + friends.size()); + System.out.println("Size is now: " + friends.size()); //1 friends.add("John"); friends.add("Jake"); friends.add("Jenny"); @@ -23,22 +24,26 @@ public static void main(String[] args) { friends.add("Jacobi"); friends.add("Juliet"); friends.add("Jeremy"); - System.out.println("Size is now: " + friends.size()); + System.out.println("Size is now: " + friends.size()); //13 //test add at index friends.add(1, "Hello"); //test add with errors // friends.add(-1, "Hello"); + //below prints with hello after Jess +// for(int i=0; i< friends.size(); i++){ +// System.out.println(friends.get(i)); +// } //test remove by item - friends.remove("Hello"); + friends.remove("Hello"); //remove Hello //test remove by index - friends.remove(0); + friends.remove(0); //remove Jess - List friends2 = new ArrayList(); + List friends2 = new SinglyLinkedList<>(); friends2.add("John"); - friends2.add("Jak"); + friends2.add("Jake"); friends2.add("Jenny"); friends2.add("Joey"); friends2.add("Jordan"); @@ -46,7 +51,7 @@ public static void main(String[] args) { friends2.add("Jill"); friends2.add("Joe"); - List friends3 = new ArrayList(); + List friends3 = new SinglyLinkedList<>(); friends3.add("Jose"); friends3.add("Jun"); friends3.add("Jeremiah"); @@ -60,42 +65,43 @@ public static void main(String[] args) { } //test isEmpty method - System.out.println("Is this array empty: " + friends.isEmpty()); + System.out.println("Is this array empty: " + friends.isEmpty()); //false //test contains method - System.out.println("This array contains John: " + friends.contains("John")); - System.out.println("This array contains Joh: " + friends.contains("Joh")); + System.out.println("This array contains John: " + friends.contains("John")); //true + System.out.println("This array contains Joh: " + friends.contains("Joh")); //false // System.out.println("This method catches null parameter: " + friends.contains(null)); //test containsAll - System.out.println(friends.containsAll(friends2)); + System.out.println("Friends array contains all of friends2? " + friends.containsAll(friends2)); //true + System.out.println("retains all below, should have 8 names"); //test retainsAll - friends.retainAll(friends2); + friends.retainAll(friends2); // for(int i=0; i< friends.size(); i++){ System.out.println(friends.get(i)); } - + System.out.println(); //test size method - System.out.println("Size is now: " + friends.size()); + System.out.println("Size is now: " + friends.size()); //now 8 System.out.println(); //test addAll - friends.addAll(friends3); + friends.addAll(friends3); //adds 3 names for(int i=0; i< friends.size(); i++){ System.out.println(friends.get(i)); } System.out.println(); //test removeAll - friends.removeAll(friends3); + friends.removeAll(friends3); //removes the 3 that were added for(int i=0; i< friends.size(); i++){ System.out.println(friends.get(i)); } System.out.println(); //test set - friends.set(0, "Jesus"); + friends.set(0, "Jesus"); //adds Jesus to front for(int i=0; i< friends.size(); i++){ System.out.println(friends.get(i)); } diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index a4da4fb..0131ae6 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -2,6 +2,7 @@ import java.util.Iterator; import java.util.ListIterator; +import java.util.NoSuchElementException; public class SinglyLinkedList implements List{ //Fields- What is in a LinkedList? @@ -199,7 +200,13 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + for (ItemType item: otherCollection + ) { + if(!this.contains(item)){ + return false; + } + } + return true; } /** @@ -225,7 +232,13 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - + for (ItemType item: otherCollection + ) { + int a = this.indexOf(item); + if(a >= 0){ + this.remove(a); + } + } } /** @@ -238,7 +251,15 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { - + List temp = new SinglyLinkedList(); + this.size = 0; + for (ItemType item: otherCollection) { + if(indexOf(item) != -1){ + temp.add(item); + size++; + } + } + this.head = ((SinglyLinkedList) temp).head; } /** @@ -277,6 +298,7 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + isNull(item); validIndex(index); Node current = head; @@ -304,6 +326,7 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { + isNull(item); validIndex(index); Node temp = new Node(); Node current = head; @@ -436,7 +459,7 @@ public OurEnhancedIterator(){ */ @Override public boolean hasNext() { - return currentPosition != null; + return currentPosition.next != null; } /** @@ -451,7 +474,13 @@ public boolean hasNext() { */ @Override public ItemType next() { - return null; + if(!hasNext()){ + throw new NoSuchElementException(); + } + ItemType next = currentPosition.data; + currentPosition = currentPosition.next; + return next; + } /** From 8de467852211fc893e2c6f6e2ba2f06401aa88d6 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 6 Feb 2023 14:24:12 -0800 Subject: [PATCH 19/20] worked on recursive methods for linked list --- src/Main.java | 18 ++ .../sdev333/RecursiveLinkedList.java | 281 ++++++++++++++++++ 2 files changed, 299 insertions(+) create mode 100644 src/edu/greenriver/sdev333/RecursiveLinkedList.java diff --git a/src/Main.java b/src/Main.java index 4f82932..02a2ad4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,6 @@ import edu.greenriver.sdev333.ArrayList; import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.RecursiveLinkedList; import edu.greenriver.sdev333.SinglyLinkedList; import java.util.Iterator; @@ -30,6 +31,7 @@ public static void main(String[] args) { friends.add(1, "Hello"); //test add with errors // friends.add(-1, "Hello"); + System.out.println("Index of Hello = 1? " + friends.indexOf("Hello")); //below prints with hello after Jess // for(int i=0; i< friends.size(); i++){ @@ -108,6 +110,22 @@ public static void main(String[] args) { //test indexOf System.out.println("Index of Jesus: " + friends.indexOf("Jesus")); + System.out.println( ); + + + List friends4 = new RecursiveLinkedList<>(); + System.out.println("List size before add: " + friends4.size()); + + friends4.add("John"); + friends4.add("Jake"); + friends4.add("Jenny"); + friends4.add("Joey"); + friends4.add("Jordan"); + friends4.add("Justin"); + friends4.add("Jill"); + friends4.add("Joe"); + + System.out.println("List size after add: " + friends4.size()); } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/RecursiveLinkedList.java b/src/edu/greenriver/sdev333/RecursiveLinkedList.java new file mode 100644 index 0000000..645b325 --- /dev/null +++ b/src/edu/greenriver/sdev333/RecursiveLinkedList.java @@ -0,0 +1,281 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class RecursiveLinkedList implements List{ + //private helper class + private class Node{ + ItemType data; + Node next; + } + //helper method to break list down to head/rest + //does the actual work + private int size(Node theHead){ + //base case to end recursion + if(theHead == null){ + return 0; + } + else{ + // theHead.next = "the rest" + return size(theHead.next) +1; + } + } + //fields + private int size; + private Node head; + + public RecursiveLinkedList(){ + head = null; + } + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() { + return size(head); + } + + /** + * 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) { + //add item to back of list + head = helpAdd(head, item); + + } + + //recursively get to the end of the list *HELPER CLASS FOR ADD* + private Node helpAdd(Node theHead, ItemType item){ + if(theHead == null){ + //add new node here + Node theNewNode = new Node(); + theNewNode.data = item; + theNewNode.next = null; + return theNewNode; + } + else{ + theHead.next = helpAdd(theHead.next, item); + return theHead; + } + } + + /** + * 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) { + if(index == 0){ + //add to front + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next= this.head; + head = theNewOne; + } + + } + + /** + * 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 40b04c7b0edf881a32e875af5afd3cefed93d3b1 Mon Sep 17 00:00:00 2001 From: Katherine Watkins Date: Mon, 6 Feb 2023 14:24:32 -0800 Subject: [PATCH 20/20] worked on recursive methods for linked list --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 0131ae6..7487e09 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -340,10 +340,8 @@ public void add(int index, ItemType item) { current = current.next; counter++; } - temp.data = item; temp.next = current.next; - current.next = temp; } size++;