From bb32aa3b3d17a83e608875399fb7bee394a24eb5 Mon Sep 17 00:00:00 2001 From: cloverspark Date: Wed, 18 Jan 2023 21:32:58 -0800 Subject: [PATCH 1/6] new method --- src/Main.java | 9 +++++---- src/edu/greenriver/sdev333/ArrayList.java | 21 +++++++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index d9154a2..e93ebb5 100644 --- a/src/Main.java +++ b/src/Main.java @@ -22,8 +22,9 @@ public static void main(String[] args) { friends.add("Dorothy"); friends.add("Sophia"); friends.add(2, "Wednesday"); + friends.add("Tina"); System.out.println("size is now " + friends.size()); - + System.out.println(friends.lastIndexOf("Tina")); //for (int i = 0; i < friends.size(); i++) { // System.out.println(friends.get(i)); //} @@ -36,9 +37,9 @@ public static void main(String[] args) { } - for (String name : friends) { - System.out.println(name); - } + //for (String name : friends) { + // System.out.println(name); + //} diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 483ec57..31a2faa 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -15,7 +15,7 @@ public class ArrayList implements List { // size is DIFFERENT than length private int size; - + // construction public ArrayList() { size = 0; data = (ItemType[]) new Object[10]; @@ -179,7 +179,15 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not gonna do it!"); + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + + + //ItemType itemToCheck = itr.next(); + + Collection.class.cast(itr.next()); + } + } /** @@ -192,7 +200,7 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - + throw new UnsupportedOperationException("Not gonna do it!"); } /** @@ -205,7 +213,7 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { - + throw new UnsupportedOperationException("Not gonna do it!"); } /** @@ -321,6 +329,11 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { + for (int i = size; i > 0; i--) { + if (item.equals(data[i])) { + return i; + } + } return 0; } From cd65b325924d76f60105481e5475bd18029e3fc3 Mon Sep 17 00:00:00 2001 From: cloverspark Date: Thu, 19 Jan 2023 00:16:01 -0800 Subject: [PATCH 2/6] new methods and try on retainsall --- src/Main.java | 19 ++++++++++++++++--- src/edu/greenriver/sdev333/ArrayList.java | 23 ++++++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Main.java b/src/Main.java index e93ebb5..c11cdb9 100644 --- a/src/Main.java +++ b/src/Main.java @@ -7,7 +7,7 @@ public static void main(String[] args) { System.out.println("Hello world!"); List friends = new ArrayList(); - + List friends2 = new ArrayList(); System.out.println("initial size is " + friends.size()); friends.add("Jess"); @@ -23,8 +23,13 @@ public static void main(String[] args) { friends.add("Sophia"); friends.add(2, "Wednesday"); friends.add("Tina"); + friends2.add("daw"); + friends2.add("hi"); + friends2.add("Tina"); + friends2.add("Rose"); System.out.println("size is now " + friends.size()); System.out.println(friends.lastIndexOf("Tina")); + //for (int i = 0; i < friends.size(); i++) { // System.out.println(friends.get(i)); //} @@ -35,8 +40,16 @@ public static void main(String[] args) { String name = itr.next(); System.out.println(name); } - - + friends.addAll(friends2); + System.out.println("size is now " +friends.size()); + friends.removeAll(friends2); + System.out.println("size is now " +friends.size()); + System.out.println(friends2.size()); + Iterator itrs = friends2.iterator(); + while (itrs.hasNext()) { + String name = itrs.next(); + System.out.println(name); + } //for (String name : friends) { // System.out.println(name); //} diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 31a2faa..34ea6a8 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -185,7 +185,7 @@ public void addAll(Collection otherCollection) { //ItemType itemToCheck = itr.next(); - Collection.class.cast(itr.next()); + add(itr.next()); } } @@ -200,7 +200,14 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not gonna do it!"); + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + + + //ItemType itemToCheck = itr.next(); + + remove(itr.next()); + } } /** @@ -213,7 +220,17 @@ public void removeAll(Collection otherCollection) { */ @Override public void retainAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not gonna do it!"); + Iterator itr = (Iterator)otherCollection.iterator(); + while (itr.hasNext()) { + + + ItemType itemToCheck = itr.next(); + if (contains(itemToCheck)) { + this.remove(itemToCheck); + } + + } + } /** From b7e5fb9cf4120d869c55166f2d6776517f25f3e7 Mon Sep 17 00:00:00 2001 From: cloverspark Date: Thu, 19 Jan 2023 09:53:20 -0800 Subject: [PATCH 3/6] new methods and try on retainAll thinks solved --- src/Main.java | 12 +++++++++--- src/edu/greenriver/sdev333/ArrayList.java | 9 ++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Main.java b/src/Main.java index c11cdb9..c7c7ad4 100644 --- a/src/Main.java +++ b/src/Main.java @@ -45,17 +45,23 @@ public static void main(String[] args) { friends.removeAll(friends2); System.out.println("size is now " +friends.size()); System.out.println(friends2.size()); + System.out.println(friends2.contains("Tina")); + friends.retainAll(friends2); + System.out.println("size is now " +friends.size()); + Iterator itrs = friends2.iterator(); while (itrs.hasNext()) { String name = itrs.next(); System.out.println(name); } - //for (String name : friends) { - // System.out.println(name); - //} + for (String name : friends) { + System.out.println(name); + } + + } diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 34ea6a8..46a803c 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -221,16 +221,15 @@ public void removeAll(Collection otherCollection) { @Override public void retainAll(Collection otherCollection) { Iterator itr = (Iterator)otherCollection.iterator(); + Collection collection = new ArrayList(); while (itr.hasNext()) { - - ItemType itemToCheck = itr.next(); if (contains(itemToCheck)) { - this.remove(itemToCheck); + collection.add(itemToCheck); } - } - + clear(); + addAll(collection); } /** From cc3d1bc566305772cd10253d067249639ca2746a Mon Sep 17 00:00:00 2001 From: cloverspark Date: Thu, 19 Jan 2023 16:43:07 -0800 Subject: [PATCH 4/6] ListIterator started but not fully there --- src/edu/greenriver/sdev333/ArrayList.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 46a803c..0de3bd8 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -397,34 +397,38 @@ public SecondCustomIterator() { @Override public boolean hasNext() { - return false; + return currentPosition < size(); } @Override public ItemType next() { - return null; + ItemType result = get(currentPosition); + currentPosition++; + return result; } @Override public boolean hasPrevious() { // hasNext checked currentPosition with size // hasPrevious check currentPosition against 0 - return false; + return currentPosition > 0; } @Override public ItemType previous() { - return null; + ItemType result = get(currentPosition); + currentPosition--; + return result; } @Override public int nextIndex() { - return 0; + return currentPosition+1; } @Override public int previousIndex() { - return 0; + return currentPosition -1; } @Override From b28c1c309660da0779cbf549903d021ce3d23b53 Mon Sep 17 00:00:00 2001 From: cloverspark Date: Thu, 19 Jan 2023 21:59:30 -0800 Subject: [PATCH 5/6] added some checks --- src/edu/greenriver/sdev333/ArrayList.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 0de3bd8..a4ca75f 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -181,7 +181,7 @@ public boolean containsAll(Collection otherCollection) { public void addAll(Collection otherCollection) { Iterator itr = (Iterator)otherCollection.iterator(); while (itr.hasNext()) { - + checkSize(); //ItemType itemToCheck = itr.next(); @@ -242,7 +242,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if (index >= size) { + if (index >= size||index<0) { throw new IndexOutOfBoundsException("index is beyond size"); } @@ -262,7 +262,7 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - if (index >= size) { + if (index >= size||index<0) { throw new IndexOutOfBoundsException("index is beyond size"); } @@ -303,6 +303,9 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + if (index >= size||index<0) { + throw new IndexOutOfBoundsException("index is beyond size"); + } // shift values left to overwrite the item at index for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; @@ -362,7 +365,7 @@ public int lastIndexOf(ItemType item) { */ @Override public ListIterator listIterator() { - return null; + return new SecondCustomIterator(); } @@ -390,9 +393,10 @@ public ItemType next() { private class SecondCustomIterator implements ListIterator { // fancier Iterator - lets us go forwards and backwards private int currentPosition; - + private int lastPosition; public SecondCustomIterator() { currentPosition = 0; + lastPosition=-1; } @Override From 4845a435cba9fe71246ddb6d13c86684d2eb381e Mon Sep 17 00:00:00 2001 From: cloverspark Date: Mon, 13 Feb 2023 16:57:54 -0800 Subject: [PATCH 6/6] methods done and some basic test set --- src/Main.java | 23 +- .../greenriver/sdev333/SinglyLinkedList.java | 393 ++++++++++++++++++ 2 files changed, 413 insertions(+), 3 deletions(-) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/Main.java b/src/Main.java index c7c7ad4..2c44ab6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -60,9 +60,26 @@ public static void main(String[] args) { for (String name : friends) { System.out.println(name); } + System.out.println(); + System.out.println(); - - - + List linked1 = new SinglyLinkedList<>(); + linked1.add("Tina1"); + linked1.add("Josh1"); + linked1.add("Susan1"); + linked1.add("Tyler1"); + linked1.add("Usman1"); + linked1.add("Dee1"); + linked1.add("Rose1"); + linked1.add("Blanche1"); + linked1.add("Dorothy1"); + linked1.add(3,"wadwe"); + int haha=linked1.indexOf("wadwe"); + System.out.println(haha); + Iterator itrss = linked1.iterator(); + while (itrss.hasNext()) { + String name = itrss.next(); + System.out.println(name); + } } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java new file mode 100644 index 0000000..3f78cb7 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,393 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class SinglyLinkedList implements List { + //Fields + private Node head; + private int size; + + //helper/inner classes + private class Node{ + ItemType data; + Node next; + } + + //constructor + public SinglyLinkedList(){ + size = 0; + head = null; + } + //helper method + private void checkIndex(int index){ + if(index<0 || index>= size){ + throw new IndexOutOfBoundsException(); + } + } + /** + * 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||head==null; + } + + /** + * 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 + */ + public boolean contains(ItemType item) { + //create node to hold our place + Node current = head; + while(current != null){ + if(current.data == item){ + return true; + } + current = current.next; + } + 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) { + Node newNode = new Node(); + newNode.data = item; + newNode.next = null; + if(head == null){ + head = newNode; + }else { + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + current.next = newNode; + } + size++; + } + + /** + * 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) { + if(item == null){ + throw new NullPointerException(); + } + + /* + alternative slower method with less code: + int position = indexOf(item); + if(position != -1){ + remove(position); + } + */ + + 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 -=1; + } + + /** + * Removes all items from this collection. + * The collection will be empty after this method returns. + */ + @Override + public void clear() { + size = 0; + head = null; + } + + /** + * 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) { + for(ItemType item: otherCollection) { + if (!this.contains(item)) { + 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) { // did in class + Iterator itr = (Iterator)otherCollection.iterator(); + while(itr.hasNext()){ + ItemType currentItem = itr.next(); + add(0,currentItem); + } + } + + /** + * 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) { + Iterator itr = (Iterator)otherCollection.iterator(); + while(itr.hasNext()) { + ItemType currentItem = itr.next(); + remove(currentItem); + } + } + + /** + * 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) { + checkIndex(index); + Node current = head; + int counter = 0; + while(counter != index){ + current = current.next; + counter++; + } + return current.data; + } + + /** + * 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) { + checkIndex(index); + Node current = head; + int counter = 0; + while(counter!= index){ + current = current.next; + counter++; + } + current.data = 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) { + checkIndex(index); + //if the head is null + if (index == 0) { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + current.next = theNewOne; + size++; + } + } + + /** + * 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) { + checkIndex(index); + if(index == 0){ + head = head.next; + } + else { + Node current = head; + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + current.next = current.next.next; + } + 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) { + int counter = 0; + Node current =head; + while (current != null) { + + if(current.data.equals(item)){ + return counter; + } + counter++; + current = current.next; + } + + //if we get here, it's not found + 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; + } + private class OurCustomIterator implements Iterator{ + + //field to keep track of position + private Node currentPosition; + + public OurCustomIterator(){ + currentPosition= head; + } + @Override + public boolean hasNext() { + //Teacher solution + if(currentPosition != null){ + return true; + } + return false; + } + + @Override + public ItemType next() { + //Teacher solution + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + } +} +