From 24168abf39f037cd747587c5b13f4c869b7fcc82 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 11 Jan 2023 14:28:34 -0800 Subject: [PATCH 1/9] Implemented the constructor, size , add. --- .idea/vcs.xml | 6 + src/Main.java | 20 +- src/edu/greenriver/sdev333/ArrayList.java | 284 ++++++++++++++++++++++ 3 files changed, 309 insertions(+), 1 deletion(-) 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..02aa082 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,23 @@ +import edu.greenriver.sdev333.*; + public class Main { - public static void main(String[] args) { + public static void main(String[] args) + { System.out.println("Hello world!"); + + List friends = new ArrayList(); + System.out.println("Initial size is " + friends.size()); + friends.add("Jess"); + friends.add("Tina"); + friends.add("Josh"); + friends.add("Susan"); + friends.add("Tyler"); + friends.add("Usman"); + friends.add("Dee"); + friends.add("Rose"); + friends.add("Blanche"); + friends.add("Dorothy"); + friends.add("Sophia"); + System.out.println("size is now " + friends.size()); } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java new file mode 100644 index 0000000..4b49de3 --- /dev/null +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -0,0 +1,284 @@ +package edu.greenriver.sdev333; + +import java.lang.reflect.Array; +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List +{ + //WE NEED FIELDS!! + + //one plain old Java array + + private ItemType[] data; + + // one int to keep track of size + // size is the # of spots that are used in the data array + // size is different than length + private int size; + + + public ArrayList() + { + size = 0; + data = (ItemType[]) new Object[10]; + + } + + + + /** + * Returns the number of items in this collection. + * + * @return the number of items in this collection + */ + @Override + public int size() + { + return size; + } + + /** + * Returns true if this collection contains no items. + * + * @return true if this collection contains no items + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns true if this collection contains the specified item. + * + * @param item items whose presence in this collection is to be tested + * @return true if this collection contains the specified item + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public boolean contains(ItemType item) { + return false; + } + + /** + * Returns an iterator over the elements in this collection. + * + * @return an Iterator over the elements in this collection + */ + @Override + public Iterator iterator() { + return null; + } + + /** + * Adds the specified item to the collection. + * + * @param item item to be added to the collection + * @throws NullPointerException if the specified item is null + * and this collection does not permit null items + */ + @Override + public void add(ItemType item) + { + if(size == data.length) + { + //resize up (double up the array size) + + // step 1 - create a new larger array + ItemType[] temp = (ItemType[]) new Object[size * 2]; + + // Step 2 = copy items from data to temp + for(int i = 0 ; i < data.length ; i++) + { + temp[i] = data[i]; + } + + //Step 3 - repoint/ reference data to point to new array + data = temp; + + // Optional: + //overwrite + temp = null; + } //end of if (need to resize) + data[size] = item; + size++; + + }// end of method + + // all of above code works until i run out of room. + // when size becomes the same as length, I'm out of room + + + /** + * 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 cdcff5cf5757999dc1956882483a124a2b6d8ac5 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 11 Jan 2023 14:36:10 -0800 Subject: [PATCH 2/9] Implemented the constructor, size , add. --- src/edu/greenriver/sdev333/ArrayList.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 4b49de3..7e24276 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -45,7 +45,13 @@ public int size() */ @Override public boolean isEmpty() { + if(size == 0) + { + return true; + } return false; + + //alternate way to write it : return size == 0 ; } /** From eb822bd3a9eb1a8cafe86801593aebcd92532241 Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 21 Jan 2023 19:38:19 -0800 Subject: [PATCH 3/9] changes --- .idea/misc.xml | 2 +- src/Main.java | 19 +- src/edu/greenriver/sdev333/ArrayList.java | 202 +++++++++++++++++++--- 3 files changed, 199 insertions(+), 24 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 07115cd..05b1176 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 02aa082..f25ba16 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,5 @@ import edu.greenriver.sdev333.*; - +import java.util.Iterator; public class Main { public static void main(String[] args) { @@ -18,6 +18,23 @@ public static void main(String[] args) friends.add("Blanche"); friends.add("Dorothy"); friends.add("Sophia"); + friends.add(2,"Wednesday"); System.out.println("size is now " + friends.size()); + + //for (int i = 0; i < friends.size(); i ++) + // System.out.println(friends.get(i)) + Iterator itr = friends.iterator(); + while(itr.hasNext()) + { + String name = itr.next(); + System.out.println(name); + } + for(String name : friends) + { + System.out.println(name); + } + + } + } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 7e24276..832e4c0 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -22,7 +22,6 @@ public ArrayList() { size = 0; data = (ItemType[]) new Object[10]; - } @@ -63,7 +62,13 @@ public boolean isEmpty() { * and this collection does not permit null items */ @Override - public boolean contains(ItemType item) { + public boolean contains(ItemType item) + { + int i = indexOf(item); + if( i != -1) + { + return true; + } return false; } @@ -74,18 +79,9 @@ public boolean contains(ItemType item) { */ @Override public Iterator iterator() { - return null; + 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) + private void checkSize() { if(size == data.length) { @@ -107,6 +103,20 @@ public void add(ItemType item) //overwrite temp = null; } //end of if (need to resize) + }// end of method + + /** + * 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++; @@ -126,7 +136,12 @@ public void add(ItemType item) */ @Override public void remove(ItemType item) { - + int i = indexOf(item); + if(i != -1) + { + // if it's found, use the other remove method to do the work + remove(i); + } } /** @@ -135,7 +150,7 @@ public void remove(ItemType item) { */ @Override public void clear() { - + size = 0; } /** @@ -148,9 +163,28 @@ public void clear() { */ @Override public boolean containsAll(Collection otherCollection) { - return false; - } + // fail fast (fail loud) + Iterator itr = (Iterator)otherCollection.iterator(); + while(itr.hasNext()) + { + ItemType itemToCheck = itr.next(); + if(!contains(itemToCheck)) + { + return false; + } + } + return true; + +/* for(ItemType itemtoCheck : otherCollection) + { + if(!contains(itemTocheck)) + { + return false; + } + }*/ + // return false; + } /** * Adds all the items in this specified other collection to this collection. * @@ -158,7 +192,7 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - + throw new UnsupportedOperationException("Not going to do it"); } /** @@ -197,7 +231,11 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - return null; + if(index >= size) + { + throw new IndexOutOfBoundsException("index is beyond size"); + } + return data[index]; } /** @@ -213,7 +251,12 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + if(index >= size) + { + throw new IndexOutOfBoundsException("index is beyond size"); + } + data[index] = item; } /** @@ -230,7 +273,14 @@ public void set(int index, ItemType item) { */ @Override public void add(int index, ItemType item) { - + checkSize(); + //focus on trying this one ... + for(int i = size; i >= index + 1; i --) + { + data[i] = data[i - 1]; + } + data[index] = item; + size++; } /** @@ -243,7 +293,13 @@ public void add(int index, ItemType item) { */ @Override public void remove(int index) { + //shift values left to overwrite the item at index + for(int i = index; i < size -1; i ++) + { + data[i] = data[i + 1]; + } + size--; } /** @@ -258,7 +314,19 @@ public void remove(int index) { */ @Override public int indexOf(ItemType item) { - return 0; + + for(int i = 0 ; i 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 SecondCustomIterator implements ListIterator + { + + @Override + public boolean hasNext() { + return false; + } + + @Override + public ItemType next() { + return null; + } + + @Override + public boolean hasPrevious() + { + // hasNext checked currentPosition with size + // hasPrevious check currentPosition against 0 + return false; + } + + @Override + public ItemType previous() { + return null; + } + + @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) { + + } + } + +}// end of class ArrayList From 4621d447aab2ae7a556ed2848d9d85b264b41b86 Mon Sep 17 00:00:00 2001 From: zhenhuaizeng Date: Sun, 22 Jan 2023 16:37:56 -0800 Subject: [PATCH 4/9] finished --- src/Main.java | 106 +++++++++++- src/edu/greenriver/sdev333/ArrayList.java | 200 ++++++++++++++-------- 2 files changed, 231 insertions(+), 75 deletions(-) diff --git a/src/Main.java b/src/Main.java index f25ba16..01241bc 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,9 +1,11 @@ import edu.greenriver.sdev333.*; import java.util.Iterator; +import java.util.ListIterator; + 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()); @@ -20,7 +22,6 @@ public static void main(String[] args) friends.add("Sophia"); friends.add(2,"Wednesday"); System.out.println("size is now " + friends.size()); - //for (int i = 0; i < friends.size(); i ++) // System.out.println(friends.get(i)) Iterator itr = friends.iterator(); @@ -32,7 +33,108 @@ public static void main(String[] args) for(String name : friends) { System.out.println(name); + }*/ + + //test (Method main (or jUnit tests) need to be present to demonstrate that you have tested what you have wrote) + + //Constructor: ArrayList() + List friends = new ArrayList(); + + //public void add(ItemType item) + friends.add("Jack"); + + //public int size() Should return 1 + System.out.println("Size of the Array : " + friends.size()); + + //public boolean isEmpty() Should return false + System.out.println("Is the Array Empty? " + friends.isEmpty()); + + //public boolean contains(ItemType item) - hint: this method can use indexOf to do its work + System.out.println("Does the Array contain Jack? " + friends.contains("Jack")); + + //public int indexOf(ItemType item) + System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); + + //public void add(int index, ItemType item) + friends.add(0,"Ken"); + System.out.println("Check if Ken is added to my frendsList, Ken's index is " + friends.indexOf("Ken")); + System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); + + //public void remove(int index) + friends.remove(1); + //test this method + System.out.println("Is Jack still in the List? " + friends.contains("Jack")); + + //public ItemType get(int index) + System.out.println(friends.get(0)); + + //public void set(int index, ItemType item) + friends.set(0,"Jack"); + System.out.println("Now Ken changed to " + friends.get(0)); + + //public void clear() + friends.clear(); + System.out.println("Nothing in the Array List, the size of Friends is " + friends.size()); + + //public Iterator iterator() + friends.add("Jack"); + friends.add("Ken"); + friends.add("Brandon"); + Iterator itr = friends.iterator(); + while(itr.hasNext()) + { + String name = itr.next(); + System.out.println(name); + } + + //Level 2 Requirements (to earn beyond 75-80% credit, up to 100%, if working properly) + //Second constructor: ArrayList(int capacity) - allows the client to ask for an initial capacity for the underlying data array (instead of using our default of 10) + List friend = new ArrayList(5); + friend.add("JackOne"); + System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); + + //public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work + friend.remove("JackOne"); + System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); + + //**public int lastIndexOf(ItemType item) + friends.add("Jack"); + System.out.println("the last index of Jack should be 3: which is : " + friends.lastIndexOf("Jack")); + + //**public void addAll(Collection otherCollection) + friend.add("Jack"); + friend.add("Ken"); + friend.add("Brandon"); + friends.addAll(friend); + + //**public boolean equals(Object otherObject) + System.out.println(friends.equals(friend)); + friend.clear(); + friends.clear(); + System.out.println(friends.equals(friend)); + friends.add("Jack"); + friend.add("Jack"); + System.out.println(friends.equals(friend)); + //public ListIterator listIterator() + friends.add("Ken"); + friends.add("Brandon"); + ListIterator itrL = friends.listIterator(); + while(itrL.hasNext()) + { + String name = itrL.next(); + System.out.println(name); + System.out.println(itrL.nextIndex()); } + while(itrL.hasPrevious()) + { + String name = itrL.previous(); + System.out.println(name); + System.out.println(itrL.previousIndex()); + } + + //**Move resizing code into its own private method, something like: private void resize(int newSize) + //Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) + // those two methods are written in ArrayList. } diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 832e4c0..e34e117 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -4,8 +4,7 @@ import java.util.Iterator; import java.util.ListIterator; -public class ArrayList implements List -{ +public class ArrayList implements List { //WE NEED FIELDS!! //one plain old Java array @@ -18,12 +17,15 @@ public class ArrayList implements List private int size; - public ArrayList() - { - size = 0; - data = (ItemType[]) new Object[10]; + public ArrayList() { + size = 0; + data = (ItemType[]) new Object[10]; } + public ArrayList(int capacity) { + data = (ItemType[]) new Object[capacity]; + + } /** @@ -32,8 +34,7 @@ public ArrayList() * @return the number of items in this collection */ @Override - public int size() - { + public int size() { return size; } @@ -44,8 +45,7 @@ public int size() */ @Override public boolean isEmpty() { - if(size == 0) - { + if (size == 0) { return true; } return false; @@ -62,11 +62,9 @@ public boolean isEmpty() { * and this collection does not permit null items */ @Override - public boolean contains(ItemType item) - { + public boolean contains(ItemType item) { int i = indexOf(item); - if( i != -1) - { + if (i != -1) { return true; } return false; @@ -81,18 +79,17 @@ public boolean contains(ItemType item) public Iterator iterator() { return new OurCustomIterator(); } - private void checkSize() - { - if(size == data.length) - { + + /*** Move resizing code into its own private method, something like: private void resize(int newSize) */ + private void checkSize() { + if (size == data.length) { //resize up (double up the array size) // step 1 - create a new larger array ItemType[] temp = (ItemType[]) new Object[size * 2]; // Step 2 = copy items from data to temp - for(int i = 0 ; i < data.length ; i++) - { + for (int i = 0; i < data.length; i++) { temp[i] = data[i]; } @@ -104,7 +101,6 @@ private void checkSize() temp = null; } //end of if (need to resize) }// end of method - /** * Adds the specified item to the collection. * @@ -113,10 +109,8 @@ private void checkSize() * and this collection does not permit null items */ @Override - public void add(ItemType item) - { + public void add(ItemType item) { checkSize(); - data[size] = item; size++; @@ -137,8 +131,7 @@ public void add(ItemType item) @Override public void remove(ItemType item) { int i = indexOf(item); - if(i != -1) - { + if (i != -1) { // if it's found, use the other remove method to do the work remove(i); } @@ -164,12 +157,10 @@ public void clear() { @Override public boolean containsAll(Collection otherCollection) { // fail fast (fail loud) - Iterator itr = (Iterator)otherCollection.iterator(); - while(itr.hasNext()) - { + Iterator itr = (Iterator) otherCollection.iterator(); + while (itr.hasNext()) { ItemType itemToCheck = itr.next(); - if(!contains(itemToCheck)) - { + if (!contains(itemToCheck)) { return false; } } @@ -185,6 +176,7 @@ public boolean containsAll(Collection otherCollection) { }*/ // return false; } + /** * Adds all the items in this specified other collection to this collection. * @@ -192,7 +184,19 @@ public boolean containsAll(Collection otherCollection) { */ @Override public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException("Not going to do it"); + int num = 0; + List dataOne = new ArrayList(otherCollection.size()); + Iterator itr = (Iterator) otherCollection.iterator(); + while(itr.hasNext()) + { + checkSize(); + ItemType name = itr.next(); + dataOne.add(name); + data[size] = dataOne.get(num); + size++; + num++; + } + } /** @@ -231,13 +235,20 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if(index >= size) - { - throw new IndexOutOfBoundsException("index is beyond size"); - } + isValidIndex(index); return data[index]; } + + /*Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index)*/ + + private boolean isValidIndex(int index) + { + if (index < 0 || index >= size()) { + throw new IndexOutOfBoundsException("index is beyond size"); + } + return true; + } /** * Replaces the item at the specified position in this list * with the specified item @@ -251,10 +262,7 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { - if(index >= size) - { - throw new IndexOutOfBoundsException("index is beyond size"); - } + isValidIndex(index); data[index] = item; } @@ -275,8 +283,7 @@ public void set(int index, ItemType item) { public void add(int index, ItemType item) { checkSize(); //focus on trying this one ... - for(int i = size; i >= index + 1; i --) - { + for (int i = size; i >= index + 1; i--) { data[i] = data[i - 1]; } data[index] = item; @@ -294,8 +301,7 @@ public void add(int index, ItemType item) { @Override public void remove(int index) { //shift values left to overwrite the item at index - for(int i = index; i < size -1; i ++) - { + for (int i = index; i < size - 1; i++) { data[i] = data[i + 1]; } @@ -315,13 +321,10 @@ public void remove(int index) { @Override public int indexOf(ItemType item) { - for(int i = 0 ; i listIterator() { - return null; + return new SecondCustomIterator(); } - private class OurCustomIterator implements Iterator + @Override + public boolean equals(Object otherObject) { + if(otherObject == this) + { + return true; + } + if (!(otherObject instanceof ArrayList)) { + return false; + } + + // typecast o to Complex so that we can compare data members + ArrayList c = (ArrayList) otherObject; + + // Compare the data members and return accordingly + int sizeOne = 0; + if(size > ((ArrayList) otherObject).size()) + { + sizeOne = size; + } + else + { + sizeOne = ((ArrayList) otherObject).size(); + } + for(int i = 0; i ) otherObject).size() < size()) + { + return false; + } + if(!data[i].equals(((ArrayList) otherObject).get(i))) + { + return false; + } + } + return true; + + } + private class OurCustomIterator implements Iterator { //fields private int currentPosition; - public OurCustomIterator() - { + + public OurCustomIterator() { currentPosition = 0; } /** @@ -373,8 +422,7 @@ public OurCustomIterator() * @return {@code true} if the iteration has more elements */ @Override - public boolean hasNext() - { + public boolean hasNext() { return currentPosition < size(); } @@ -382,53 +430,59 @@ public boolean hasNext() * Returns the next element in the iteration. * * @return the next element in the iteration - //* @throws NoSuchElementException if the iteration has no more elements + * //* @throws NoSuchElementException if the iteration has no more elements */ @Override - public ItemType next() - { + public ItemType next() { ItemType result = get(currentPosition); currentPosition++; return result; } } - private class SecondCustomIterator implements ListIterator - { + private class SecondCustomIterator implements ListIterator { + + + private int currentPosition; + public SecondCustomIterator(){ + currentPosition = 0; + } @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; + public boolean hasPrevious() { + return currentPosition > 0; } @Override public ItemType previous() { - return null; + currentPosition--; + ItemType result = get(currentPosition); + return result; } @Override public int nextIndex() { - return 0; + int result = currentPosition; + return result; } @Override public int previousIndex() { - return 0; + int result = currentPosition; + return result; } - @Override public void remove() { From 2666a2f3c2ed97fd9cc56848f25127bdf684decd Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 25 Jan 2023 14:44:49 -0800 Subject: [PATCH 5/9] changes#2 --- src/Main.java | 6 ++--- src/edu/greenriver/sdev333/ArrayList.java | 30 ++++++++--------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/Main.java b/src/Main.java index 01241bc..7b6acff 100644 --- a/src/Main.java +++ b/src/Main.java @@ -108,13 +108,13 @@ public static void main(String[] args) friends.addAll(friend); //**public boolean equals(Object otherObject) - System.out.println(friends.equals(friend)); + System.out.println("Are they equal?" + friends.equals(friend)); friend.clear(); friends.clear(); - System.out.println(friends.equals(friend)); + System.out.println("Are they equal?" + friends.equals(friend)); friends.add("Jack"); friend.add("Jack"); - System.out.println(friends.equals(friend)); + System.out.println("Are they equal?" + friends.equals(friend)); //public ListIterator listIterator() friends.add("Ken"); friends.add("Brandon"); diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index e34e117..6c09085 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -378,33 +378,23 @@ public boolean equals(Object otherObject) if (!(otherObject instanceof ArrayList)) { return false; } + ArrayList other = (ArrayList) otherObject; - // typecast o to Complex so that we can compare data members - ArrayList c = (ArrayList) otherObject; - - // Compare the data members and return accordingly - int sizeOne = 0; - if(size > ((ArrayList) otherObject).size()) + if(size != other.size) { - sizeOne = size; + return false; } - else + + for(int i =0; i ) otherObject).size(); - } - for(int i = 0; i ) otherObject).size() < size()) - { - return false; - } - if(!data[i].equals(((ArrayList) otherObject).get(i))) - { - return false; - } + if(!data[i].equals(other.data[i])) + { + return false; + } } return true; + } private class OurCustomIterator implements Iterator { From 2cd8c9e3b32a483b8f87e7c42ce6c45db19fc24e Mon Sep 17 00:00:00 2001 From: jack Date: Mon, 30 Jan 2023 14:46:54 -0800 Subject: [PATCH 6/9] changes#3 --- .../greenriver/sdev333/SinglyLinkedList.java | 498 ++++++++++++++++++ 1 file changed, 498 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..a927265 --- /dev/null +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -0,0 +1,498 @@ +package edu.greenriver.sdev333; + +import javax.naming.OperationNotSupportedException; +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() { + // an empty list has no nodes, + // which means it has no head, so set head to null + 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) { + //assume Indexof is working + int position = indexOf(item); + if(position == -1) + { + return false; + } + return true; + } + + /** + * 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 + /* + * 1 create the node + * put data in node + * set next to null + * head gets the addr of new node + * size++ + * */ + public void add(ItemType item) { + //if size ==0 + //the index at the 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); + if(item == null) + { + throw new NullPointerException(); + } + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = null; + if(head == null) + { + head = theNewOne; + } + else + { + //get to back. last item in list + Node current = head; + while(current.next != null) + { + current = current.next; + } + //current is pointing to last node + //add new node at end + current.next = theNewOne; + //add new node at end + } + 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) { + 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) { + 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) { + + } + + /** + * Retains only the items in this collection that are contained in the + * specified other collection. In other words, removes from this collection + * all of its items that are not contained in the specified other collection + * + * @param otherCollection collection containing elements to be retained in + * this collection + */ + @Override + public void retainAll(Collection otherCollection) { + + } + + /** + * Returns the item at the specified position in this list + * + * @param index index of the item to return + * @return the item at the specified position in this list + * @throws IndexOutOfBoundsException if this index is out of range + * (index < 0 || index >= size()) + */ + @Override + public ItemType get(int index) { + if(index < 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) { + + } + + /** + * 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; + } + 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; + } + + //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) + { + 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; + for(int i = 0; i < index -1; i ++) + { + current = current.next; + } + + //when I get here - current is pointing to the node BEFORE the on 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(); + } + + private class OurCustomIterator implements Iterator + { + + //field + private Node currentPosition; + public OurCustomIterator() + { + currentPosition = head; + } + + @Override + public boolean hasNext() { + // see if I'm on the last node: if (current.next = null) + // see if I made it past the last node: if( current == null) + if(currentPosition != null) + { + return true; + } + return false; + } + + @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; + } + + @Override + public boolean hasNext() { + return currentPosition != null; + } + + @Override + public ItemType next() { + ItemType result = currentPosition.data; + currentPosition = currentPosition.next; + return result; + } + + @Override + public boolean hasPrevious() { + return false; + } + + @Override + public ItemType previous() { + return null; + } + + @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) { + + } + } + + +} \ No newline at end of file From 6601105a3b2df68940a4353dc3e0debae3e717d8 Mon Sep 17 00:00:00 2001 From: jack Date: Wed, 1 Feb 2023 14:51:37 -0800 Subject: [PATCH 7/9] changes#4 --- src/edu/greenriver/sdev333/SinglyLinkedList.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index a927265..3c779f5 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -198,8 +198,19 @@ public boolean containsAll(Collection otherCollection) { * @param otherCollection collection containing items to be added to this collection */ @Override - public void addAll(Collection otherCollection) { - throw new UnsupportedOperationException(); + public void addAll(Collection otherCollection) + { + //walk through the other collection + // for-each loop + // or use Iterator + + Iterator itr = (Iterator) otherCollection.iterator(); + while(itr.hasNext()) + { + ItemType currentItem = itr.next(); + this.add(0,currentItem); + } + } /** From 166da4cc153121b5f5b6dbdb9cb07e4e4b069177 Mon Sep 17 00:00:00 2001 From: jack Date: Tue, 7 Feb 2023 20:09:00 -0800 Subject: [PATCH 8/9] changes#5 --- .../greenriver/sdev333/SinglyLinkedList.java | 134 +++++++++++++++--- 1 file changed, 112 insertions(+), 22 deletions(-) diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index 3c779f5..d0121d6 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -84,14 +84,14 @@ public Iterator iterator() { */ @Override /* - * 1 create the node - * put data in node - * set next to null - * head gets the addr of new node - * size++ - * */ + * 1 create the node + * put data in node + * set next to null + * head gets the addr of new node + * size++ + * */ public void add(ItemType item) { - //if size ==0 + //if size ==0 //the index at the 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 @@ -115,9 +115,9 @@ public void add(ItemType item) { { current = current.next; } - //current is pointing to last node - //add new node at end - current.next = theNewOne; + //current is pointing to last node + //add new node at end + current.next = theNewOne; //add new node at end } size++; @@ -145,7 +145,6 @@ public void remove(ItemType item) { remove(position); } /* - if(head.data == item) { head = head.next; @@ -188,8 +187,27 @@ public void clear() { * in the specified other collection */ @Override - public boolean containsAll(Collection otherCollection) { - return false; + public boolean containsAll(Collection otherCollection) + { +/* for(ItemType item : otherCollection) + { + if(!this.contains(item)) + { + return false; + } + } + return true;*/ + + Iterator itr = (Iterator) otherCollection.iterator(); + while(itr.hasNext()) + { + ItemType item = itr.next(); + if(!contains(item)) + { + return false; + } + } + return true; } /** @@ -278,6 +296,26 @@ public ItemType get(int index) { */ @Override public void set(int index, ItemType item) { + checkIndex(index); + if(item == null) + { + throw new NullPointerException(); + } + if(index == 0) + { + // if someone wants to add at the beginning, i need to change the head + head.data = item; + } + else + { + Node current = head; + //stop at the position i want to insert at + for(int i = 0; i < index; i ++) + { + current = current.next; + } + current.data = item; + } } @@ -324,6 +362,15 @@ public void add(int index, ItemType item) } size++; } + + public void addToTheHead(ItemType item) + { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + } + private void checkIndex(int index) { if(index < 0 || index >= size) @@ -402,9 +449,52 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - return 0; + if(item == null) + { + throw new NullPointerException(); + } + int element = -1; + int counter = -1; + Node current = head; + while(current != null) + { + element++; + if(current.data.equals(item)) + { + counter = element; + } + current = current.next; + } + + return counter; } + @Override + public boolean equals(Object otherObject) + { + if(otherObject == null) + { + return false; + } + SinglyLinkedList other = (SinglyLinkedList) otherObject; + if(size != other.size) + { + return false; + } + + Node current = head; + Node currentOne = other.head; + while(current != null) + { + if(!current.data.equals(currentOne.data)) + { + return false; + } + current = current.next; + currentOne = currentOne.next; + } + return true; + } /** * Returns a list iterator over the elements in this list * (in proper sequence). @@ -471,39 +561,39 @@ public ItemType next() { @Override public boolean hasPrevious() { - return false; + throw new UnsupportedOperationException(); } @Override public ItemType previous() { - return null; + throw new UnsupportedOperationException(); } @Override public int nextIndex() { - return 0; + throw new UnsupportedOperationException(); } @Override public int previousIndex() { - return 0; + throw new UnsupportedOperationException(); } @Override public void remove() { - + throw new UnsupportedOperationException(); } @Override public void set(ItemType itemType) { - + throw new UnsupportedOperationException(); } @Override public void add(ItemType itemType) { - + throw new UnsupportedOperationException(); } } -} \ No newline at end of file +}//End of class SinglyLinkedList \ No newline at end of file From 71e0f396583d6352ff9a884c02a3de00f02911e5 Mon Sep 17 00:00:00 2001 From: jack Date: Mon, 13 Feb 2023 16:58:31 -0800 Subject: [PATCH 9/9] changes#6 --- src/Main.java | 236 +++++++++++++----- src/edu/greenriver/sdev333/ArrayList.java | 35 ++- .../greenriver/sdev333/SinglyLinkedList.java | 180 ++++++------- 3 files changed, 273 insertions(+), 178 deletions(-) diff --git a/src/Main.java b/src/Main.java index 7b6acff..46dd41e 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,7 +5,7 @@ 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()); @@ -37,106 +37,216 @@ public static void main(String[] args) //test (Method main (or jUnit tests) need to be present to demonstrate that you have tested what you have wrote) - //Constructor: ArrayList() - List friends = new ArrayList(); +// //Constructor: ArrayList() +// List friends = new ArrayList(); +// +// //public void add(ItemType item) +// friends.add("Jack"); +// +// //public int size() Should return 1 +// System.out.println("Size of the Array : " + friends.size()); +// +// //public boolean isEmpty() Should return false +// System.out.println("Is the Array Empty? " + friends.isEmpty()); +// +// //public boolean contains(ItemType item) - hint: this method can use indexOf to do its work +// System.out.println("Does the Array contain Jack? " + friends.contains("Jack")); +// +// //public int indexOf(ItemType item) +// System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); +// +// //public void add(int index, ItemType item) +// friends.add(0,"Ken"); +// System.out.println("Check if Ken is added to my frendsList, Ken's index is " + friends.indexOf("Ken")); +// System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); +// +// //public void remove(int index) +// friends.remove(1); +// //test this method +// System.out.println("Is Jack still in the List? " + friends.contains("Jack")); +// +// //public ItemType get(int index) +// System.out.println(friends.get(0)); +// +// //public void set(int index, ItemType item) +// friends.set(0,"Jack"); +// System.out.println("Now Ken changed to " + friends.get(0)); +// +// //public void clear() +// friends.clear(); +// System.out.println("Nothing in the Array List, the size of Friends is " + friends.size()); +// +// //public Iterator iterator() +// friends.add("Jack"); +// friends.add("Ken"); +// friends.add("Brandon"); +// Iterator itr = friends.iterator(); +// while(itr.hasNext()) +// { +// String name = itr.next(); +// System.out.println(name); +// } +// +// //Level 2 Requirements (to earn beyond 75-80% credit, up to 100%, if working properly) +// //Second constructor: ArrayList(int capacity) - allows the client to ask for an initial capacity for the underlying data array (instead of using our default of 10) +// List friend = new ArrayList(5); +// friend.add("JackOne"); +// System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); +// +// //public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work +// friend.remove("JackOne"); +// System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); +// +// //**public int lastIndexOf(ItemType item) +// friends.add("Jack"); +// System.out.println("the last index of Jack should be 3: which is : " + friends.lastIndexOf("Jack")); +// +// //**public void addAll(Collection otherCollection) +// friend.add("Jack"); +// friend.add("Ken"); +// friend.add("Brandon"); +// friends.addAll(friend); +// +// //**public boolean equals(Object otherObject) +// System.out.println(friends.equals(friend)); +// friend.clear(); +// friends.clear(); +// System.out.println(friends.equals(friend)); +// friends.add("Jack"); +// friend.add("Jack"); +// System.out.println(friends.equals(friend)); +// //public ListIterator listIterator() +// friends.add("Ken"); +// friends.add("Brandon"); +// ListIterator itrL = friends.listIterator(); +// while(itrL.hasNext()) +// { +// String name = itrL.next(); +// System.out.println(name); +// System.out.println(itrL.nextIndex()); +// } +// while(itrL.hasPrevious()) +// { +// String name = itrL.previous(); +// System.out.println(name); +// System.out.println(itrL.previousIndex()); +// } + + //**Move resizing code into its own private method, something like: private void resize(int newSize) + //Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) + // those two methods are written in ArrayList. + + + + + + /*SinglyLinkedList + * Level 1 Requirements (to earn 75-80% credit if working properly) + Provide an implementation for the following methods: +* */ + //Constructor: LinkedList() + SinglyLinkedList friendss = new SinglyLinkedList(); //public void add(ItemType item) - friends.add("Jack"); + friendss.add("Jack"); - //public int size() Should return 1 - System.out.println("Size of the Array : " + friends.size()); + //public int size() + System.out.println(friendss.size()); - //public boolean isEmpty() Should return false - System.out.println("Is the Array Empty? " + friends.isEmpty()); + //public boolean isEmpty() + System.out.println(friendss.isEmpty()); //public boolean contains(ItemType item) - hint: this method can use indexOf to do its work - System.out.println("Does the Array contain Jack? " + friends.contains("Jack")); + System.out.println("Does the SinglyLinkedList contain Jack? : " + friendss.contains("Jack")); //public int indexOf(ItemType item) - System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); + System.out.println(friendss.indexOf("Jack")); + + //public void add(ItemType item) + friendss.add("Brandon"); //public void add(int index, ItemType item) - friends.add(0,"Ken"); - System.out.println("Check if Ken is added to my frendsList, Ken's index is " + friends.indexOf("Ken")); - System.out.println("Index of 'Jack' is " + friends.indexOf("Jack")); + friendss.add(1,"John"); - //public void remove(int index) - friends.remove(1); - //test this method - System.out.println("Is Jack still in the List? " + friends.contains("Jack")); + //public void remove(int index) Jack should be removed + friendss.remove(0); //public ItemType get(int index) - System.out.println(friends.get(0)); + System.out.println(friendss.get(0)); //public void set(int index, ItemType item) - friends.set(0,"Jack"); - System.out.println("Now Ken changed to " + friends.get(0)); - - //public void clear() - friends.clear(); - System.out.println("Nothing in the Array List, the size of Friends is " + friends.size()); + friendss.set(0,"Josh"); //public Iterator iterator() - friends.add("Jack"); - friends.add("Ken"); - friends.add("Brandon"); - Iterator itr = friends.iterator(); + System.out.println(); + Iterator itr = friendss.iterator(); while(itr.hasNext()) { String name = itr.next(); System.out.println(name); } + //public void clear() + friendss.clear(); + System.out.println(friendss.size()); //Level 2 Requirements (to earn beyond 75-80% credit, up to 100%, if working properly) - //Second constructor: ArrayList(int capacity) - allows the client to ask for an initial capacity for the underlying data array (instead of using our default of 10) - List friend = new ArrayList(5); - friend.add("JackOne"); - System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); //public void remove(ItemType item) - hint: this method can use indexOf, then remove(index) to do its work - friend.remove("JackOne"); - System.out.println("Is JackOne in the List? " + friend.contains("JackOne")); + System.out.println(); + friendss.add("Jack"); + friendss.add("Josh"); + friendss.add("Brandon"); + friendss.remove("Jack"); + for(String name : friendss) + { + System.out.println(name); + } //**public int lastIndexOf(ItemType item) - friends.add("Jack"); - System.out.println("the last index of Jack should be 3: which is : " + friends.lastIndexOf("Jack")); + //the Index of Josh should be 2 + friendss.add("Josh"); + System.out.println("The lastIndexOf Josh should be 2 : " + friendss.lastIndexOf("Josh")); //**public void addAll(Collection otherCollection) - friend.add("Jack"); - friend.add("Ken"); - friend.add("Brandon"); - friends.addAll(friend); - - //**public boolean equals(Object otherObject) - System.out.println("Are they equal?" + friends.equals(friend)); - friend.clear(); - friends.clear(); - System.out.println("Are they equal?" + friends.equals(friend)); - friends.add("Jack"); - friend.add("Jack"); - System.out.println("Are they equal?" + friends.equals(friend)); - //public ListIterator listIterator() - friends.add("Ken"); - friends.add("Brandon"); - ListIterator itrL = friends.listIterator(); - while(itrL.hasNext()) + SinglyLinkedList friendsss = new SinglyLinkedList<>(); + friendsss.add("Josh"); + friendsss.add("Brandon"); + friendsss.add("Josh"); + friendss.addAll(friendsss); + System.out.println(); + for (String name : friendss) { - String name = itrL.next(); System.out.println(name); - System.out.println(itrL.nextIndex()); } - while(itrL.hasPrevious()) + + //**public boolean equals(Object otherObject) + System.out.println(); + System.out.println(friendss.equals(friendsss)); + friendsss.add("Josh"); + friendsss.add("Brandon"); + friendsss.add("Josh"); + System.out.println(); + System.out.println(friendss.equals(friendsss)); + + //public ListIterator listIterator() + System.out.println(); + ListIterator itrOne = friendsss.listIterator(); + while(itrOne.hasNext()) { - String name = itrL.previous(); + String name = itrOne.next(); System.out.println(name); - System.out.println(itrL.previousIndex()); } - //**Move resizing code into its own private method, something like: private void resize(int newSize) //Move code to validate indexes into its own private method, something like: private boolean isValidIndex(int index) - // those two methods are written in ArrayList. - - + /* + private void checkIndex(int index) + { + if (index < 0 || index >= size) + { + throw new IndexOutOfBoundsException(); + } + } + */ } } \ No newline at end of file diff --git a/src/edu/greenriver/sdev333/ArrayList.java b/src/edu/greenriver/sdev333/ArrayList.java index 6c09085..93c926d 100644 --- a/src/edu/greenriver/sdev333/ArrayList.java +++ b/src/edu/greenriver/sdev333/ArrayList.java @@ -24,7 +24,6 @@ public ArrayList() { public ArrayList(int capacity) { data = (ItemType[]) new Object[capacity]; - } @@ -115,7 +114,18 @@ public void add(ItemType item) { size++; }// end of method - + public void addToFront(ItemType item) + { + checkSize(); + ItemType[] temp = (ItemType[]) new Object[data.length+1]; + temp[0] = item; + for(int i =0; i listIterator() { @Override public boolean equals(Object otherObject) { - if(otherObject == this) - { - return true; - } + if(otherObject == this) + { + return true; + } if (!(otherObject instanceof ArrayList)) { return false; } @@ -382,20 +392,19 @@ public boolean equals(Object otherObject) if(size != other.size) { - return false; + return false; } for(int i =0; i { //fields diff --git a/src/edu/greenriver/sdev333/SinglyLinkedList.java b/src/edu/greenriver/sdev333/SinglyLinkedList.java index d0121d6..16a50bf 100644 --- a/src/edu/greenriver/sdev333/SinglyLinkedList.java +++ b/src/edu/greenriver/sdev333/SinglyLinkedList.java @@ -58,8 +58,7 @@ public boolean isEmpty() { public boolean contains(ItemType item) { //assume Indexof is working int position = indexOf(item); - if(position == -1) - { + if (position == -1) { return false; } return true; @@ -96,23 +95,18 @@ public void add(ItemType item) { //example: if list is size 5, last index is 4 //so we can just insert at the last index //add(size()-1,item); - if(item == null) - { + if (item == null) { throw new NullPointerException(); } Node theNewOne = new Node(); theNewOne.data = item; theNewOne.next = null; - if(head == null) - { + if (head == null) { head = theNewOne; - } - else - { + } else { //get to back. last item in list Node current = head; - while(current.next != null) - { + while (current.next != null) { current = current.next; } //current is pointing to last node @@ -123,6 +117,34 @@ public void add(ItemType item) { size++; } +/* public void addToTheBack(ItemType item) + { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = null; + if (head == null) { + head = theNewOne; + } else { + //get to back. last item in list + Node current = head; + while (current.next != null) { + current = current.next; + } + //current is pointing to last node + //add new node at end + current.next = theNewOne; + //add new node at end + } + size++; + } + + public void addToTheHead(ItemType item) { + Node theNewOne = new Node(); + theNewOne.data = item; + theNewOne.next = head; + head = theNewOne; + size++; + }*/ /** * Removes a single instance of the specified item from this collection, @@ -134,14 +156,12 @@ public void add(ItemType item) { */ @Override public void remove(ItemType item) { - if(item == null) - { + if (item == null) { throw new NullPointerException(); } //alternative - easier to write, but less efficient int position = indexOf(item); - if (position != -1) - { + if (position != -1) { remove(position); } /* @@ -187,8 +207,7 @@ public void clear() { * in the specified other collection */ @Override - public boolean containsAll(Collection otherCollection) - { + public boolean containsAll(Collection otherCollection) { /* for(ItemType item : otherCollection) { if(!this.contains(item)) @@ -199,11 +218,9 @@ public boolean containsAll(Collection otherCollection) return true;*/ Iterator itr = (Iterator) otherCollection.iterator(); - while(itr.hasNext()) - { + while (itr.hasNext()) { ItemType item = itr.next(); - if(!contains(item)) - { + if (!contains(item)) { return false; } } @@ -216,17 +233,15 @@ public boolean containsAll(Collection otherCollection) * @param otherCollection collection containing items to be added to this collection */ @Override - public void addAll(Collection otherCollection) - { + public void addAll(Collection otherCollection) { //walk through the other collection // for-each loop // or use Iterator Iterator itr = (Iterator) otherCollection.iterator(); - while(itr.hasNext()) - { + while (itr.hasNext()) { ItemType currentItem = itr.next(); - this.add(0,currentItem); + this.add(currentItem); } } @@ -267,14 +282,12 @@ public void retainAll(Collection otherCollection) { */ @Override public ItemType get(int index) { - if(index < 0 || index >= size) - { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } Node current = head; int counter = 0; - while(counter != index) - { + while (counter != index) { current = current.next; counter++; } @@ -297,21 +310,16 @@ public ItemType get(int index) { @Override public void set(int index, ItemType item) { checkIndex(index); - if(item == null) - { + if (item == null) { throw new NullPointerException(); } - if(index == 0) - { + if (index == 0) { // if someone wants to add at the beginning, i need to change the head head.data = item; - } - else - { + } else { Node current = head; //stop at the position i want to insert at - for(int i = 0; i < index; i ++) - { + for (int i = 0; i < index; i++) { current = current.next; } current.data = item; @@ -332,25 +340,19 @@ public void set(int index, ItemType item) { * (index < 0 || index >= size()) */ @Override - public void add(int index, ItemType item) - { + public void add(int index, ItemType item) { checkIndex(index); - if(index == 0) - { + 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; - } - else - { + } else { Node current = head; //stop one before the position i want to insert at - for(int i = 0; i < index - 1; i ++) - { + for (int i = 0; i < index - 1; i++) { current = current.next; } @@ -363,21 +365,13 @@ public void add(int index, ItemType item) size++; } - public void addToTheHead(ItemType item) - { - Node theNewOne = new Node(); - theNewOne.data = item; - theNewOne.next = head; - head = theNewOne; - } - private void checkIndex(int index) - { - if(index < 0 || index >= 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. @@ -387,18 +381,13 @@ private void checkIndex(int index) * (index < 0 || index >= size()) */ @Override - public void remove(int index) - { + public void remove(int index) { checkIndex(index); - if(index == 0 ) - { + if (index == 0) { head = head.next; - } - else - { + } else { Node current = head; - for(int i = 0; i < index -1; i ++) - { + for (int i = 0; i < index - 1; i++) { current = current.next; } @@ -406,7 +395,6 @@ public void remove(int index) current.next = current.next.next; } - size--; } @@ -424,10 +412,8 @@ public void remove(int index) public int indexOf(ItemType item) { int counter = 0; Node current = head; - while(current != null) - { - if(current.data.equals(item)) - { + while (current != null) { + if (current.data.equals(item)) { return counter; } counter++; @@ -449,18 +435,15 @@ public int indexOf(ItemType item) { */ @Override public int lastIndexOf(ItemType item) { - if(item == null) - { + if (item == null) { throw new NullPointerException(); } int element = -1; int counter = -1; Node current = head; - while(current != null) - { + while (current != null) { element++; - if(current.data.equals(item)) - { + if (current.data.equals(item)) { counter = element; } current = current.next; @@ -468,25 +451,21 @@ public int lastIndexOf(ItemType item) { return counter; } + @Override - public boolean equals(Object otherObject) - { - if(otherObject == null) - { + public boolean equals(Object otherObject) { + if (otherObject == null) { return false; } SinglyLinkedList other = (SinglyLinkedList) otherObject; - if(size != other.size) - { + if (size != other.size) { return false; } Node current = head; Node currentOne = other.head; - while(current != null) - { - if(!current.data.equals(currentOne.data)) - { + while (current != null) { + if (!current.data.equals(currentOne.data)) { return false; } current = current.next; @@ -495,6 +474,7 @@ public boolean equals(Object otherObject) return true; } + /** * Returns a list iterator over the elements in this list * (in proper sequence). @@ -507,13 +487,12 @@ public ListIterator listIterator() { return new OurEnhancedIterator(); } - private class OurCustomIterator implements Iterator - { + private class OurCustomIterator implements Iterator { //field private Node currentPosition; - public OurCustomIterator() - { + + public OurCustomIterator() { currentPosition = head; } @@ -521,8 +500,7 @@ public OurCustomIterator() public boolean hasNext() { // see if I'm on the last node: if (current.next = null) // see if I made it past the last node: if( current == null) - if(currentPosition != null) - { + if (currentPosition != null) { return true; } return false; @@ -536,13 +514,12 @@ public ItemType next() { } } - private class OurEnhancedIterator implements ListIterator - { + private class OurEnhancedIterator implements ListIterator { private Node currentPosition; private int currentIndex; - public OurEnhancedIterator() - { + + public OurEnhancedIterator() { currentPosition = head; currentIndex = 0; } @@ -595,5 +572,4 @@ public void add(ItemType itemType) { } } - -}//End of class SinglyLinkedList \ No newline at end of file +} \ No newline at end of file