From 64ca76d88fc313aa9054a8ea219f15d810a35a1e Mon Sep 17 00:00:00 2001 From: adamwise Date: Wed, 11 Jan 2023 14:28:11 -0800 Subject: [PATCH 1/7] added the ArrayList.add() method, and tested in main to make sure it works --- .idea/vcs.xml | 6 + src/Main.java | 21 ++ src/edu/greenriver/sdev333/Arraylist.java | 274 ++++++++++++++++++++++ 3 files changed, 301 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/Main.java b/src/Main.java index 3e59c38..31da933 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,26 @@ +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"); + friends.add("adam"); + friends.add("de"); + friends.add("kevin"); + friends.add("carl"); + friends.add("alex"); + friends.add("tony"); + friends.add("meadow"); + friends.add("aj"); + 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 new file mode 100644 index 0000000..440fd2b --- /dev/null +++ b/src/edu/greenriver/sdev333/Arraylist.java @@ -0,0 +1,274 @@ +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 number of elements that are not null in the array + //size is different from length + private int size; + + // constructor + + 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 the above code works until i run out of room + // when size becomes the same as length ,i'm out of room! + // so we must run an if statement to check + 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 for data to temp + for (int i = 0; i < size; i++) { + temp[i] = data[i]; + } + + //Step 3 - repoint/reference data to point to new array + data = temp; + // Optional + temp = null; + + }// end of if need to re + data[size] = item; + 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) { + + } + + /** + * 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 2822f36c4b573b98a7a7c6c3c188e51251b5cabf Mon Sep 17 00:00:00 2001 From: adamwise Date: Wed, 11 Jan 2023 14:49:14 -0800 Subject: [PATCH 2/7] added get and set methods --- src/Main.java | 3 +++ src/edu/greenriver/sdev333/Arraylist.java | 24 +++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Main.java b/src/Main.java index 31da933..b1b0e37 100644 --- a/src/Main.java +++ b/src/Main.java @@ -19,8 +19,11 @@ public static void main(String[] args) { friends.add("meadow"); friends.add("aj"); friends.add("Jess"); + friends.add("Jess"); System.out.println("size is now " + friends.size()); + + // friends.addAll(); will throw exception..fail fast } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/Arraylist.java b/src/edu/greenriver/sdev333/Arraylist.java index 440fd2b..c600f9b 100644 --- a/src/edu/greenriver/sdev333/Arraylist.java +++ b/src/edu/greenriver/sdev333/Arraylist.java @@ -29,7 +29,7 @@ public Arraylist(){ */ @Override public int size() { - return size; + return size ; } /** @@ -39,7 +39,7 @@ public int size() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -120,6 +120,8 @@ public void remove(ItemType item) { @Override public void clear() { + // lazy deletion - length will still be whatever it last was + size = 0; } /** @@ -132,7 +134,10 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; + + //fail fast(fail loud) + throw new UnsupportedOperationException(" Not Implemented"); + //return false; } /** @@ -142,7 +147,7 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - + throw new UnsupportedOperationException(" Not Implemented"); } /** @@ -181,7 +186,11 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + + if(index >= size){ + throw new IndexOutOfBoundsException(); + } + return data[index]; } /** @@ -197,7 +206,10 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - + if(index >= size){ + throw new IndexOutOfBoundsException(); + } + data[index] = item; } /** From ff476506f547bea6b89e2d86311cc3a28dc3869a Mon Sep 17 00:00:00 2001 From: adamwise Date: Wed, 18 Jan 2023 14:46:19 -0800 Subject: [PATCH 3/7] implemented more methods, like add(index, --- src/Main.java | 48 +++- src/edu/greenriver/sdev333/Arraylist.java | 264 +++++++++++++++++++--- 2 files changed, 274 insertions(+), 38 deletions(-) diff --git a/src/Main.java b/src/Main.java index b1b0e37..bc059d1 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,13 +1,16 @@ import edu.greenriver.sdev333.Arraylist; import edu.greenriver.sdev333.List; +import java.util.Iterator; + +import javax.imageio.metadata.IIOMetadataFormatImpl; public class Main { public static void main(String[] args) { - System.out.println("Hello world!"); + //System.out.println("Hello world!"); List friends = new Arraylist(); - System.out.println("Initial size is " + friends.size()); + System.out.println("Friends ArrayList initial size is " + friends.size()); friends.add("Jess"); friends.add("adam"); @@ -17,13 +20,48 @@ public static void main(String[] args) { friends.add("alex"); friends.add("tony"); friends.add("meadow"); - friends.add("aj"); - friends.add("Jess"); - friends.add("Jess"); + System.out.println("The Array list returns true if Jess in found within: " +friends.contains("Jess")); + System.out.println(friends); + System.out.println("size is now " + friends.size()); + System.out.println("The index of Jess is " +friends.indexOf("Jess")); + + System.out.println(); + friends.add(8,"karly"); + System.out.println(friends); + System.out.println("size is now " + friends.size()+" and it should be 9"); + System.out.println(); + friends.add(0,"frank"); + System.out.println(friends); + System.out.println("size is now " + friends.size()+" and it should be 10"); + System.out.println(friends.indexOf("kevin")); + friends.remove(0); + System.out.println(friends); System.out.println("size is now " + friends.size()); + friends.add(7,"tanner"); + System.out.println(friends); + System.out.println("size is now " + friends.size()); + + System.out.println();// seperation space + + for (int i = 0; i < friends.size(); i++) { + System.out.println(friends.get(i)); + + } + + System.out.println(); + + Iterator itr = friends.iterator(); + while(itr.hasNext()){ + String name= itr.next(); + System.out.println(name); + } + System.out.println(); + for( String name: friends){ + System.out.println(name); + } // friends.addAll(); will throw exception..fail fast } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/Arraylist.java b/src/edu/greenriver/sdev333/Arraylist.java index c600f9b..0dc8dd4 100644 --- a/src/edu/greenriver/sdev333/Arraylist.java +++ b/src/edu/greenriver/sdev333/Arraylist.java @@ -21,6 +21,11 @@ public Arraylist(){ data =(ItemType[]) new Object[10]; } + public Arraylist(int intCapacity){ + size = 0; + data =(ItemType[]) new Object[intCapacity]; + } + /** * Returns the number of items in this collection. @@ -29,6 +34,7 @@ public Arraylist(){ */ @Override public int size() { + return size ; } @@ -39,6 +45,7 @@ public int size() { */ @Override public boolean isEmpty() { + return size == 0; } @@ -52,9 +59,15 @@ public boolean isEmpty() { */ @Override public boolean contains(ItemType item) { + + // create an enhanced for loop to check each item in the list for the item being passed through + // to the contains method + for (ItemType E : data) { + if (E.equals(item)) + return true; + } return false; } - /** * Returns an iterator over the elements in this collection. * @@ -62,22 +75,10 @@ public boolean contains(ItemType item) { */ @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 the above code works until i run out of room - // when size becomes the same as length ,i'm out of room! - // so we must run an if statement to check + return new OurCustomIterator(); + } + public void checkSize(){ if(size == data.length){ //***********resize up(double up the array size**************// @@ -91,15 +92,33 @@ public void add(ItemType item) { //Step 3 - repoint/reference data to point to new array data = temp; - // Optional + temp = null; + }// end of if need to re + } + + /** + * 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++; } + + + + /** * Removes a single instance of the specified item from this collection, * if it is present. @@ -111,6 +130,17 @@ public void add(ItemType item) { @Override public void remove(ItemType item) { + for (int i = 0; i < size; i++) { + if(data[i].equals(item)){ + for (int j = i; j < size -1; j++) { + data[j] = data[j +1]; + } + data[size-1] = null; + size--; + + } + + } } /** @@ -134,10 +164,24 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { +// Iterator itr = (Iterator) otherCollection.iterator(); +// while(itr.hasNext()){ +// ItemType itemToCheck = itr.next(); +// if(!contains(itemToCheck)){ +// return false; +// } +// } +// +// return true; + +// this is the other way to write this method + for (ItemType itemToCheck : otherCollection) { + if (!contains(itemToCheck)) { + return false; + } - //fail fast(fail loud) - throw new UnsupportedOperationException(" Not Implemented"); - //return false; + } + return true; } /** @@ -227,20 +271,47 @@ public void set(int index, ItemType item) { @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) { + checkSize(); + for (int i = size ; i >= index +1; i--) { + data[i] = data[i-1]; + + + } + data[index] = item; + 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){ + + //create a loop to iterate through the array + for (int i = 0; i < size; i++) { + // conditional looks to see if the parameter passed is in the array + if (i == index) { + // if the parameter exists in the array, we iterate through the list again, as many times as there are elements in our array + for (int j = i; j < size - 1; j++) { + // we replace the element occupying the index we passed as parameter with the next element in line, and we shift all elements toward the head of the array + data[j] = data[j + 1]; + } + // after all items have been shift, we decrement the size of the array list + data[size - 1] = null; + size--; + + } + + } + } - } /** * Returns the index of the first occurrence of the specified item @@ -254,7 +325,19 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + + int result = 0; + + for (int i = 0; i < size; i++) { + if (item.equals(data[i])){ + result = i; + return result; + } + else{ + result = -1; + } + } + return result; } /** @@ -283,4 +366,119 @@ public int lastIndexOf(ItemType item) { public ListIterator listIterator() { return null; } + + private class OurCustomIterator implements Iterator { + + //fields + private int currentPosition; + + public OurCustomIterator(){ + currentPosition = 0; + } + + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + /** + * 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 = get(currentPosition); + currentPosition++; + return result; + } + } + + private class SecondCustomerIterator implements ListIterator{ + // Fancier ITerator - lets us go forwards and backwards + + private int currentPosition; + + public SecondCustomerIterator(){ + currentPosition = 0; + } + @Override + public boolean hasNext() { + return currentPosition < size(); + } + + @Override + public ItemType next() { + ItemType result = get(currentPosition); + currentPosition++; + return result; + } + + @Override + public boolean hasPrevious() { + //hasNext check curretnPosition with size + //hasPrevious check currentPosition against 0 + return currentPosition < 0; + } + + @Override + public ItemType previous() { + ItemType result = get(currentPosition); + currentPosition--; + return result; + } + + @Override + public int nextIndex() { + return 0; + } + + @Override + public int previousIndex() { + return 0; + } + + @Override + public void remove() { + + } + + @Override + public void set(ItemType itemType) { + + } + + @Override + public void add(ItemType itemType) { + + } + } + + // this is a method i had to create as i was not able to do a toString to check what was in my arrayListd + @Override + public String toString() { + if (size == 0) { + return "[]"; + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + for (int i = 0; i < size; i++) { + sb.append(data[i]); + if (i < size - 1) { + sb.append(", "); + } + } + sb.append("]"); + return sb.toString(); + } } + From 87f0fa1eb6ab259e773bfff9e0308901281fee2e Mon Sep 17 00:00:00 2001 From: adamwise Date: Thu, 19 Jan 2023 01:52:36 -0800 Subject: [PATCH 4/7] finished implementing and testing all methods for a 100% grade! --- src/Main.java | 102 +++++++++++++++++----- src/edu/greenriver/sdev333/Arraylist.java | 88 ++++++++++--------- 2 files changed, 131 insertions(+), 59 deletions(-) diff --git a/src/Main.java b/src/Main.java index bc059d1..b28c0ac 100644 --- a/src/Main.java +++ b/src/Main.java @@ -10,7 +10,14 @@ public static void main(String[] args) { List friends = new Arraylist(); + List nemesis = new Arraylist<>(8); + System.out.println(); System.out.println("Friends ArrayList initial size is " + friends.size()); + System.out.println("Is friends array list currently empty:"+friends.isEmpty()); + System.out.println("Nemesis initial size is " +nemesis.size()); + System.out.println("Adding friends..."); + System.out.println(); + friends.add("Jess"); friends.add("adam"); @@ -20,48 +27,103 @@ public static void main(String[] args) { friends.add("alex"); friends.add("tony"); friends.add("meadow"); - System.out.println("The Array list returns true if Jess in found within: " +friends.contains("Jess")); - System.out.println(friends); + + System.out.println("Is friends array list still empty:"+friends.isEmpty()); + System.out.println(); + + nemesis.add("Jess"); + nemesis.add("adam"); + nemesis.add("de"); + nemesis.add("tony"); + nemesis.add("kevin"); + nemesis.add("carl"); + nemesis.add("alex"); + nemesis.add("tony"); + nemesis.add("meadow"); + + System.out.println("the last occurance of tony in nemesis is index " +nemesis.lastIndexOf("tony")); + System.out.println("The friends Array list returns true if Jess in found within: " +friends.contains("Jess")); + System.out.println("The nemesis Array list returns true if kevin in found within: " +nemesis.contains("Kevin")); + + System.out.println("are all my friends my enemies?:"+nemesis.containsAll(friends)); + System.out.println(); + System.out.println("friends:"+friends); + System.out.println("nemesis:"+nemesis); + System.out.println(); System.out.println("size is now " + friends.size()); System.out.println("The index of Jess is " +friends.indexOf("Jess")); System.out.println(); - friends.add(8,"karly"); + System.out.println("adding friend karly to index 4"); + friends.add(4,"karly"); System.out.println(friends); System.out.println("size is now " + friends.size()+" and it should be 9"); System.out.println(); + System.out.println("adding frank to index 0"); friends.add(0,"frank"); System.out.println(friends); System.out.println("size is now " + friends.size()+" and it should be 10"); + System.out.println(); - System.out.println(friends.indexOf("kevin")); + System.out.println( "aye, some body get "+friends.get(4)); + + + System.out.println("kevin is at index "+friends.indexOf("kevin")); + friends.set(4,"parker"); + System.out.println("wait actually " +friends.get(4) +" is at index 4, i forgot"); + System.out.println(); + System.out.println("removing friend at index 0, and de has got to go!"); friends.remove(0); + friends.remove("de"); System.out.println(friends); System.out.println("size is now " + friends.size()); + System.out.println(); friends.add(7,"tanner"); System.out.println(friends); System.out.println("size is now " + friends.size()); - - System.out.println();// seperation space - - for (int i = 0; i < friends.size(); i++) { - System.out.println(friends.get(i)); + System.out.println("adding a few more friends"); + friends.add("Tasha"); + friends.add("Ozzy"); + System.out.println("friends now has a size of "+friends.size()); + for (int i = 0; i < 50; i++) { + friends.add(" all the homies"); } + System.out.println("Oh snap, all the homies came through!"); + System.out.println("friends now has a size of "+ friends.size()); + System.out.println("friends:"+friends); + nemesis.clear(); + System.out.println(""); + System.out.println("oh crap, you just said you liked pineapple on pizza."); + nemesis.addAll(friends); + System.out.println("nemesis:"+nemesis); + friends.removeAll(nemesis); + System.out.println("friends:"+friends); - System.out.println(); + System.out.println();// seperation space + System.out.println("all right parties over! clear out" ); + friends.clear(); + System.out.println("The friends array list now has a size of "+friends.size()+" because of your pizza peccadilloes"); - Iterator itr = friends.iterator(); - while(itr.hasNext()){ - String name= itr.next(); - System.out.println(name); - } - System.out.println(); - for( String name: friends){ - System.out.println(name); - } - // friends.addAll(); will throw exception..fail fast + // demoing how the iterator object is basically an enhanced forloop +// for (int i = 0; i < friends.size(); i++) { +// System.out.println(friends.get(i)); +// +// } +// */ +// System.out.println(); +// +// Iterator itr = friends.iterator(); +// while(itr.hasNext()){ +// String name= itr.next(); +// System.out.println(name); +// } +// System.out.println(); +// for( String name: friends){ +// System.out.println(name); +// } +// // friends.addAll(); will throw exception..fail fast } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/Arraylist.java b/src/edu/greenriver/sdev333/Arraylist.java index 0dc8dd4..5feca98 100644 --- a/src/edu/greenriver/sdev333/Arraylist.java +++ b/src/edu/greenriver/sdev333/Arraylist.java @@ -63,7 +63,7 @@ public boolean contains(ItemType item) { // create an enhanced for loop to check each item in the list for the item being passed through // to the contains method for (ItemType E : data) { - if (E.equals(item)) + if (E != null && E.equals(item)) return true; } return false; @@ -116,9 +116,6 @@ public void add(ItemType item) { } - - - /** * Removes a single instance of the specified item from this collection, * if it is present. @@ -135,7 +132,7 @@ public void remove(ItemType item) { for (int j = i; j < size -1; j++) { data[j] = data[j +1]; } - data[size-1] = null; + data[size-1] = null; size--; } @@ -191,7 +188,10 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException(" Not Implemented"); + for(ItemType itemToAdd : otherCollection){ + add(itemToAdd); + } + //throw new UnsupportedOperationException(" Not Implemented"); } /** @@ -204,7 +204,9 @@ public void addAll(Collection otherCollection) { */ @Override public void removeAll(Collection otherCollection) { - + for(ItemType itemToAdd : otherCollection){ + remove(itemToAdd); + } } /** @@ -272,45 +274,45 @@ public void set(int index, ItemType item) { public void add(int index, ItemType item) { - checkSize(); - for (int i = size ; i >= index +1; i--) { - data[i] = data[i-1]; + checkSize(); + for (int i = size ; i >= index +1; i--) { + data[i] = data[i-1]; - } - data[index] = item; - size++; } + data[index] = item; + 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){ - - //create a loop to iterate through the array - for (int i = 0; i < size; i++) { - // conditional looks to see if the parameter passed is in the array - if (i == index) { - // if the parameter exists in the array, we iterate through the list again, as many times as there are elements in our array - for (int j = i; j < size - 1; j++) { - // we replace the element occupying the index we passed as parameter with the next element in line, and we shift all elements toward the head of the array - data[j] = data[j + 1]; - } - // after all items have been shift, we decrement the size of the array list - data[size - 1] = null; - 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){ + //create a loop to iterate through the array + for (int i = 0; i < size; i++) { + // conditional looks to see if the parameter passed is in the array + if (i == index) { + // if the parameter exists in the array, we iterate through the list again, as many times as there are elements in our array + for (int j = i; j < size - 1; j++) { + // we replace the element occupying the index we passed as parameter with the next element in line, and we shift all elements toward the head of the array + data[j] = data[j + 1]; } + // after all items have been shift, we decrement the size of the array list + data[size - 1] = null; + size--; } + } + } /** @@ -337,7 +339,7 @@ public int indexOf(ItemType item) { result = -1; } } - return result; + return result; } /** @@ -352,7 +354,16 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + int lastIndex = -1; + for (int i = size-1; i >= 0 ; i--) { + if(data[i].equals(item)){ + lastIndex = i; + break; + + } + + } + return lastIndex; } /** @@ -481,4 +492,3 @@ public String toString() { return sb.toString(); } } - From 711b581968fb1d907e13c3a0ae3325e4a359e908 Mon Sep 17 00:00:00 2001 From: adamwise Date: Mon, 30 Jan 2023 14:54:26 -0800 Subject: [PATCH 5/7] made some edits, still working on creating some methods --- src/Main.java | 202 +++--- .../greenriver/sdev333/SinglyLinkedList.java | 614 ++++++++++++++++++ 2 files changed, 726 insertions(+), 90 deletions(-) create mode 100644 src/edu/greenriver/sdev333/SinglyLinkedList.java diff --git a/src/Main.java b/src/Main.java index b28c0ac..0a018d6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,9 @@ import edu.greenriver.sdev333.Arraylist; import edu.greenriver.sdev333.List; +import edu.greenriver.sdev333.SinglyLinkedList; + import java.util.Iterator; +import java.util.ListIterator; import javax.imageio.metadata.IIOMetadataFormatImpl; @@ -9,121 +12,140 @@ public static void main(String[] args) { //System.out.println("Hello world!"); - List friends = new Arraylist(); - List nemesis = new Arraylist<>(8); + List friends = new SinglyLinkedList(); + System.out.println(); - System.out.println("Friends ArrayList initial size is " + friends.size()); - System.out.println("Is friends array list currently empty:"+friends.isEmpty()); - System.out.println("Nemesis initial size is " +nemesis.size()); + System.out.println(friends); + System.out.println("Friends SingleyLinkedList initial size is " + friends.size()); + System.out.println("Is friends SingleyLinkedList currently empty:"+friends.isEmpty()); + System.out.println("Adding friends..."); System.out.println(); friends.add("Jess"); friends.add("adam"); - friends.add("de"); friends.add("kevin"); friends.add("carl"); friends.add("alex"); friends.add("tony"); friends.add("meadow"); - System.out.println("Is friends array list still empty:"+friends.isEmpty()); + System.out.println("Is friends SingleyLinkedlist still empty:"+friends.isEmpty()); System.out.println(); - nemesis.add("Jess"); - nemesis.add("adam"); - nemesis.add("de"); - nemesis.add("tony"); - nemesis.add("kevin"); - nemesis.add("carl"); - nemesis.add("alex"); - nemesis.add("tony"); - nemesis.add("meadow"); - - System.out.println("the last occurance of tony in nemesis is index " +nemesis.lastIndexOf("tony")); - System.out.println("The friends Array list returns true if Jess in found within: " +friends.contains("Jess")); - System.out.println("The nemesis Array list returns true if kevin in found within: " +nemesis.contains("Kevin")); - - System.out.println("are all my friends my enemies?:"+nemesis.containsAll(friends)); + Iterator itr = friends.iterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + + // this is not implemented yet! + ListIterator fancyItr = friends.listIterator(); + while(itr.hasNext()){ + String name = itr.next(); + System.out.println(name); + } + System.out.println(); + while(fancyItr.hasPrevious()){ + String name = fancyItr.previous(); + System.out.println(name); + } + + +// nemesis.add("Jess"); +// nemesis.add("adam"); +// nemesis.add("de"); +// nemesis.add("tony"); +// nemesis.add("kevin"); +// nemesis.add("carl"); +// nemesis.add("alex"); +// nemesis.add("tony"); +// nemesis.add("meadow"); + + // System.out.println("the last occurance of tony in nemesis is index " +nemesis.lastIndexOf("tony")); + System.out.println("The friends SingleyLinkedList returns true if Jess in found within: " +friends.contains("Jess")); + // System.out.println("The nemesis Array list returns true if kevin in found within: " +nemesis.contains("Kevin")); + + // System.out.println("are all my friends my enemies?:"+nemesis.containsAll(friends)); System.out.println(); System.out.println("friends:"+friends); - System.out.println("nemesis:"+nemesis); + // System.out.println("nemesis:"+nemesis); System.out.println(); System.out.println("size is now " + friends.size()); System.out.println("The index of Jess is " +friends.indexOf("Jess")); - System.out.println(); - System.out.println("adding friend karly to index 4"); - friends.add(4,"karly"); - System.out.println(friends); - System.out.println("size is now " + friends.size()+" and it should be 9"); - - System.out.println(); - System.out.println("adding frank to index 0"); - friends.add(0,"frank"); - System.out.println(friends); - System.out.println("size is now " + friends.size()+" and it should be 10"); - System.out.println(); - - System.out.println( "aye, some body get "+friends.get(4)); - - - System.out.println("kevin is at index "+friends.indexOf("kevin")); - friends.set(4,"parker"); - System.out.println("wait actually " +friends.get(4) +" is at index 4, i forgot"); - System.out.println(); - System.out.println("removing friend at index 0, and de has got to go!"); - friends.remove(0); - friends.remove("de"); - System.out.println(friends); - System.out.println("size is now " + friends.size()); - System.out.println(); - - friends.add(7,"tanner"); - System.out.println(friends); - System.out.println("size is now " + friends.size()); - System.out.println("adding a few more friends"); - friends.add("Tasha"); - friends.add("Ozzy"); - System.out.println("friends now has a size of "+friends.size()); - for (int i = 0; i < 50; i++) { - friends.add(" all the homies"); - - } - System.out.println("Oh snap, all the homies came through!"); - System.out.println("friends now has a size of "+ friends.size()); - System.out.println("friends:"+friends); - nemesis.clear(); - System.out.println(""); - System.out.println("oh crap, you just said you liked pineapple on pizza."); - nemesis.addAll(friends); - System.out.println("nemesis:"+nemesis); - friends.removeAll(nemesis); - System.out.println("friends:"+friends); - - System.out.println();// seperation space - System.out.println("all right parties over! clear out" ); - friends.clear(); - System.out.println("The friends array list now has a size of "+friends.size()+" because of your pizza peccadilloes"); - - // demoing how the iterator object is basically an enhanced forloop -// for (int i = 0; i < friends.size(); i++) { -// System.out.println(friends.get(i)); +// System.out.println(); +// System.out.println("adding friend karly to index 4"); +// friends.add(4,"karly"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()+" and it should be 9"); // -// } -// */ +// System.out.println(); +// System.out.println("adding frank to index 0"); +// friends.add(0,"frank"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()+" and it should be 10"); // System.out.println(); // -// Iterator itr = friends.iterator(); -// while(itr.hasNext()){ -// String name= itr.next(); -// System.out.println(name); -// } +// System.out.println( "aye, some body get "+friends.get(4)); +// +// +// System.out.println("kevin is at index "+friends.indexOf("kevin")); +// friends.set(4,"parker"); +// System.out.println("wait actually " +friends.get(4) +" is at index 4, i forgot"); +// System.out.println(); +// System.out.println("removing friend at index 0, and de has got to go!"); +// friends.remove(0); +// friends.remove("de"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()); // System.out.println(); -// for( String name: friends){ -// System.out.println(name); +// +// friends.add(7,"tanner"); +// System.out.println(friends); +// System.out.println("size is now " + friends.size()); +// System.out.println("adding a few more friends"); +// friends.add("Tasha"); +// friends.add("Ozzy"); +// System.out.println("friends now has a size of "+friends.size()); +// for (int i = 0; i < 50; i++) { +// friends.add(" all the homies"); +// // } -// // friends.addAll(); will throw exception..fail fast +// System.out.println("Oh snap, all the homies came through!"); +// System.out.println("friends now has a size of "+ friends.size()); +// System.out.println("friends:"+friends); +// nemesis.clear(); +// System.out.println(""); +// System.out.println("oh crap, you just said you liked pineapple on pizza."); +// // nemesis.addAll(friends); +// System.out.println("nemesis:"+nemesis); +// friends.removeAll(nemesis); +// System.out.println("friends:"+friends); +// +// System.out.println();// seperation space +// System.out.println("all right parties over! clear out" ); +// friends.clear(); +// System.out.println("The friends array list now has a size of "+friends.size()+" because of your pizza peccadilloes"); +// +// // demoing how the iterator object is basically an enhanced forloop +//// for (int i = 0; i < friends.size(); i++) { +//// System.out.println(friends.get(i)); +//// +//// } +//// */ +//// System.out.println(); +//// +//// Iterator itr = friends.iterator(); +//// while(itr.hasNext()){ +//// String name= itr.next(); +//// System.out.println(name); +//// } +//// System.out.println(); +//// for( String name: friends){ +//// System.out.println(name); +//// } +//// // friends.addAll(); will throw exception..fail fast } } \ 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..a4e64f3 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,614 @@ +package edu.greenriver.sdev333; + +import java.util.Iterator; +import java.util.ListIterator; + +public class SinglyLinkedList implements List{ + // FIELDS - what does a Linked List actually have in it? + private Node head; + private int size; + + // helper/inner classes + 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) { + //create node to hold our place + Node current = head; + //while our current place does not equal null,we will run + while(current != null){ + // we will check if the data field in our current place matches the + //parameter passed in, and if it does, we return true + if(current.data == item){ + return true; + } + //if current.data does not match our parameter , we set current to point to + // the next node in the list, until current is null, at which point + //we will know without a doubt if our parameter is in our list or not + 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 OurCustomerIterator(); + } + + + + public void addHead(ItemType item){ + if(head==null){ + head = new Node(); + head.data = item; + } + } + /** + * 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) { + + + //index at thee end of the list is size -1 + //example if list is size 5, last index is 4 + // so we can just insert at the last index + //add(size() - 1, 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 - easier to write but less efficient + /*int position = indexOf(item); + if(position != -1){ + remove(position); + }*/ + + if(head.data == item){ + head = head.next; + size--; + } + 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--; + } + } + } + } + + /** + * 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) { + throw new UnsupportedOperationException(); + } + + /** + * Adds all the items in this specified other collection to this collection. + * + * @param otherCollection collection containing items to be added to this collection + */ + @Override + public void addAll(Collection otherCollection) { + + throw new UnsupportedOperationException(); + + } + + /** + * 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) { + throw new UnsupportedOperationException(); + } + + /** + * 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) { + throw new UnsupportedOperationException(); + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if(index < 0 | index >= size){ + throw new IndexOutOfBoundsException(); + } + + 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) { + //16 minutes in this is explained 1/30/23 + } + + /** + * 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(index == 0){ + // if someone wants to add at the beginning, i need to change the head + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + + head = theNewOne; + } + Node current = head; + + //stop one before the position i want to insert at + for (int i = 0; i < index - 1; i++) { + current =current.next; + } + + //when i get here, current is pointing the nbode *BEFORE* the node at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + current.next = theNewOne; + + size++; + } + + private void checkIndex(int index){ + if(index < 0 | index >= size){ + throw new IndexOutOfBoundsException(); + } + } + /** + * 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; + //stop one before the position i want to insert at + for (int i = 0; i < index - 1; i++) { + current = current.next; + } + // current is point to the node before the one at index + 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 new OurEnhancedIterator(); + } + + public String toString(){ + if( head == null){ + return "[]"; + } + Node current = head; + String result = "[" ; + while(current != null){ + result += current.data+ ","; + current = current.next; + } + return result + "]"; + } + + private class OurCustomerIterator implements Iterator{ + + //field + private Node currentPosition; + + + public OurCustomerIterator(){ + currentPosition = head; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + //see if Im on the last node: if (current.next == null) + // see if i made it past the last nde:if(current == null) + if(currentPosition != null){ + return true; + } + return false; + } + + /** + * 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; + } + } + + private class OurEnhancedIterator implements ListIterator{ + + + private Node currentPosition; + private int currentIndex; + + public OurEnhancedIterator(){ + currentPosition = head; + currentIndex = 0; + } + /** + * 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() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + + /** + * 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 cf27e89cd311a2365b80baa52ce526cc74d3be6b Mon Sep 17 00:00:00 2001 From: adamwise Date: Sat, 4 Feb 2023 01:49:35 -0800 Subject: [PATCH 6/7] 100% completed the assignemnt --- src/Main.java | 39 +++--- .../greenriver/sdev333/SinglyLinkedList.java | 119 ++++++++++++++---- 2 files changed, 119 insertions(+), 39 deletions(-) diff --git a/src/Main.java b/src/Main.java index 0a018d6..6a3d4f7 100644 --- a/src/Main.java +++ b/src/Main.java @@ -53,27 +53,38 @@ public static void main(String[] args) { } -// nemesis.add("Jess"); -// nemesis.add("adam"); -// nemesis.add("de"); -// nemesis.add("tony"); -// nemesis.add("kevin"); -// nemesis.add("carl"); -// nemesis.add("alex"); -// nemesis.add("tony"); -// nemesis.add("meadow"); - - // System.out.println("the last occurance of tony in nemesis is index " +nemesis.lastIndexOf("tony")); System.out.println("The friends SingleyLinkedList returns true if Jess in found within: " +friends.contains("Jess")); - // System.out.println("The nemesis Array list returns true if kevin in found within: " +nemesis.contains("Kevin")); - // System.out.println("are all my friends my enemies?:"+nemesis.containsAll(friends)); System.out.println(); System.out.println("friends:"+friends); - // System.out.println("nemesis:"+nemesis); + System.out.println(); System.out.println("size is now " + friends.size()); System.out.println("The index of Jess is " +friends.indexOf("Jess")); + friends.remove("Jess"); + friends.remove(0); + System.out.println( " Bye-Bye Jess and adam"); + System.out.println("what , did jess and adam actually leave? "+friends); + System.out.println("looks like it"); + System.out.println(" oh crap its the cops, everybody scram!");friends.clear(); + System.out.println(friends); + System.out.println("lets add some new friends"); + friends.add(0,"Kenny"); + friends.add(1,"Ty"); + friends.add(0,"Bobby"); + System.out.println(friends); + System.out.println("Kenny is my second friend because he sits in position:"+friends.indexOf("Kenny")); + System.out.println("And "+friends.get(0)+" is my favorite friend because they sit in position: "+friends.indexOf(friends.get(0))); + System.out.println("Actually my new second best friend is Cole now"); + friends.set(1,"Cole"); + System.out.println(friends); + List nemesis = new SinglyLinkedList(); + nemesis.addAll(friends); + System.out.println("oh no, my anti friends are here! they are jus tlike my friends but their order is reversed!"); + System.out.println(nemesis); + nemesis.add(0,"Bobby"); + System.out.println(nemesis); + System.out.println("what is the last index of bobby now?"+nemesis.lastIndexOf("Bobby")); // System.out.println(); // System.out.println("adding friend karly to index 4"); diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index a4e64f3..405a486 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 does a Linked List actually have in it? @@ -12,6 +13,7 @@ public class SinglyLinkedList implements List{ private class Node{ ItemType data; Node next; + Node previous; } /** @@ -191,7 +193,15 @@ public boolean containsAll(Collection otherCollection) { @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException(); + // //walk through the other collection + //for-each loop + // or use Iterator + // must cast the iterator + Iterator itr = (Iterator)otherCollection.iterator(); + while(itr.hasNext()){ + ItemType currentItem = itr.next(); + add(0,currentItem); + } } @@ -231,9 +241,7 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if(index < 0 | index >= size){ - throw new IndexOutOfBoundsException(); - } + checkIndex(index); Node current = head; int counter = 0; @@ -258,7 +266,17 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - //16 minutes in this is explained 1/30/23 + + checkIndex(index); + Node current = head; + int counter = 0; + while(counter!= index){ + current = current.next; + counter++; + } + current.data = item; + + } /** @@ -275,33 +293,39 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - checkIndex(index); - if(index == 0){ + checkIndex(index); + + if (head == null) { + Node theNewOne = new Node(); + theNewOne.data = item; + head = theNewOne; + } else if (index == 0) { // if someone wants to add at the beginning, i need to change the head Node theNewOne = new Node(); theNewOne.data = item; theNewOne.next = head; head = theNewOne; - } - Node current = head; + } else { + Node current = head; - //stop one before the position i want to insert at - for (int i = 0; i < index - 1; i++) { - current =current.next; - } + //stop one before the position i want to insert at + for (int i = 0; i < index - 1; i++) { + current = current.next; + } - //when i get here, current is pointing the nbode *BEFORE* the node at the index - Node theNewOne = new Node(); - theNewOne.data = item; - theNewOne.next = current.next; - current.next = theNewOne; + //when i get here, current is pointing the node *BEFORE* the node at the index + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = current.next; + current.next = theNewOne; + } size++; } private void checkIndex(int index){ - if(index < 0 | index >= size){ + if(index < 0 | index > size){ throw new IndexOutOfBoundsException(); } } @@ -371,7 +395,38 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + // since we return -1 if the item does not appear in our list, + // we can create a variable called last index to hold that value. + // we will update this variable as we traverse the linked list, and eventually + // it will hold the number of the last position our node.data=item will be in + int lastIndex = -1; + // we will create a variable to count how many positions we have traveled too, + // so this number will be updated every time we traverse through our list + // eventually we will assign last index to this number and our method will be complete + int index=0; + + // we create a new node to hold our position, and we assign this node the value of whatever our head node is + // then we go from there + Node current = head; + // this is the standard traversal loop we use to traverse through nodes in a linked list + // if current equals null, then there are no longer any more objects or nodes in our linked list, so the + //loop will stop + while (current != null){ + // this conditional if statement says if our current nodes data field is the same as the item we passed into the method + // then we will set the int variable lastIndex to the value of index + // at the start, last index is -1, so if current.data never equals item, we will exit this loop when current equals null + // and our method will return the default -1. Otherwise, if the item passed through our parameter does equal current.data + // we will update last index to the value of index. we always increment the value of index, and assign current to current.next, so + // our pointer is looking to the next node in our list. our loop will run until current equals null, so we will continue to check current.data against + //our item parameter, and if we get a hit, we will update lastIndex to our newly incremented index value everytime. + if(current.data.equals(item)){ + lastIndex = index; + } + index++; + current = current.next; + } + // we return last index every time, -1 for default but whatever the appropriate index is if we have the item in our list + return lastIndex; } /** @@ -393,7 +448,10 @@ public String toString(){ Node current = head; String result = "[" ; while(current != null){ - result += current.data+ ","; + result += current.data; + if(current.next != null){ + result+=","; + } current = current.next; } return result + "]"; @@ -444,6 +502,7 @@ private class OurEnhancedIterator implements ListIterator{ private Node currentPosition; + private Node previousPosition; private int currentIndex; public OurEnhancedIterator(){ @@ -476,6 +535,9 @@ public boolean hasNext() { */ @Override public ItemType next() { + if(!hasNext()){ + throw new NoSuchElementException(); + } ItemType result = currentPosition.data; currentPosition = currentPosition.next; return result; @@ -492,7 +554,8 @@ public ItemType next() { */ @Override public boolean hasPrevious() { - return false; + + return previous()!= null; } /** @@ -509,7 +572,12 @@ public boolean hasPrevious() { */ @Override public ItemType previous() { - return null; + if(!hasPrevious()){ + throw new NoSuchElementException(); + } + currentPosition = previousPosition; + previousPosition = previousPosition.previous; + return currentPosition.data; } /** @@ -523,7 +591,8 @@ public ItemType previous() { */ @Override public int nextIndex() { - return 0; + + return currentIndex; } /** @@ -537,7 +606,7 @@ public int nextIndex() { */ @Override public int previousIndex() { - return 0; + return currentIndex - 1; } /** From 276319c89ed04142495fd31026afd061dfb1b0b7 Mon Sep 17 00:00:00 2001 From: adamwise Date: Sat, 4 Feb 2023 01:50:31 -0800 Subject: [PATCH 7/7] 100% completed the assignemnt --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 405a486..c71de85 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -555,7 +555,7 @@ public ItemType next() { @Override public boolean hasPrevious() { - return previous()!= null; + return previousPosition != null; } /**