From efbd80d2fff2a7e615153329b477cb1b5219375c Mon Sep 17 00:00:00 2001 From: Cmatthews Date: Tue, 13 Feb 2024 09:29:25 -0800 Subject: [PATCH 1/3] stubs for array and linkedlist --- src/Array.java | 150 +++++++++++++++++++++++++++++++++++++++++ src/Linkedintlist.java | 147 ++++++++++++++++++++++++++++++++++++++++ src/Main.java | 15 ++--- 3 files changed, 304 insertions(+), 8 deletions(-) create mode 100644 src/Array.java create mode 100644 src/Linkedintlist.java diff --git a/src/Array.java b/src/Array.java new file mode 100644 index 0000000..e194c0f --- /dev/null +++ b/src/Array.java @@ -0,0 +1,150 @@ +import java.util.Iterator; + +public class Array implements IntList { + + + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + return 0; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} + diff --git a/src/Linkedintlist.java b/src/Linkedintlist.java new file mode 100644 index 0000000..27bca2c --- /dev/null +++ b/src/Linkedintlist.java @@ -0,0 +1,147 @@ +import java.util.Iterator; + +public class Linkedintlist implements IntList { + /** + * Prepends (inserts) the specified value at the front of the list (at index 0). + * Shifts the value currently at the front of the list (if any) and any + * subsequent values to the right. + * + * @param value value to be inserted + */ + @Override + public void addFront(int value) { + + } + + /** + * Appends (inserts) the specified value at the back of the list (at index size()-1). + * + * @param value value to be inserted + */ + @Override + public void addBack(int value) { + + } + + /** + * Inserts the specified value at the specified position in this list. + * Shifts the value currently at that position (if any) and any subsequent + * values to the right. + * + * @param index index at which the specified value is to be inserted + * @param value value to be inserted + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public void add(int index, int value) { + + } + + /** + * Removes the value located at the front of the list + * (at index 0), if it is present. + * Shifts any subsequent values to the left. + */ + @Override + public void removeFront() { + + } + + /** + * Removes the value located at the back of the list + * (at index size()-1), if it is present. + */ + @Override + public void removeBack() { + + } + + /** + * Removes the value at the specified position in this list. + * Shifts any subsequent values to the left. Returns the value + * that was removed from the list. + * + * @param index the index of the value to be removed + * @return the value previously at the specified position + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int remove(int index) { + return 0; + } + + /** + * Returns the value at the specified position in the list. + * + * @param index index of the value to return + * @return the value at the specified position in this list + * @throws IndexOutOfBoundsException if the index is out of range + */ + @Override + public int get(int index) { + return 0; + } + + /** + * Returns true if this list contains the specified value. + * + * @param value value whose presence in this list is to be searched for + * @return true if this list contains the specified value + */ + @Override + public boolean contains(int value) { + return false; + } + + /** + * Returns the index of the first occurrence of the specified value + * in this list, or -1 if this list does not contain the value. + * + * @param value value to search for + * @return the index of the first occurrence of the specified value in this list + * or -1 if this list does not contain the value + */ + @Override + public int indexOf(int value) { + return 0; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns the number of values in this list. + * + * @return the number of values in this list + */ + @Override + public int size() { + return 0; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/Main.java b/src/Main.java index 930198c..cafd36a 100644 --- a/src/Main.java +++ b/src/Main.java @@ -2,14 +2,13 @@ // click the icon in the gutter. public class Main { public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.printf("Hello and welcome!"); + IntList list1 = new Array(); + IntList list2 = new Linkedintlist(); - for (int i = 1; i <= 5; i++) { - //TIP Press to start debugging your code. We have set one breakpoint - // for you, but you can always add more by pressing . - System.out.println("i = " + i); - } + list1.addBack(42); + list1.addBack(82); + list1.addBack(97); + + System.out.println(list1); } } \ No newline at end of file From 24bfb00726dd988354942755b8a199bf587968cb Mon Sep 17 00:00:00 2001 From: Cmatthews Date: Fri, 15 Mar 2024 12:31:23 -0700 Subject: [PATCH 2/3] stubs for array and linkedlist --- IntListReview.iml | 26 ++++ src/Array.java | 149 +++++++++++++++++- src/ArrayTest.java | 86 +++++++++++ src/Linkedintlist.java | 336 +++++++++++++++++++++++------------------ src/Main.java | 26 +++- 5 files changed, 466 insertions(+), 157 deletions(-) create mode 100644 src/ArrayTest.java diff --git a/IntListReview.iml b/IntListReview.iml index c90834f..d8a1435 100644 --- a/IntListReview.iml +++ b/IntListReview.iml @@ -7,5 +7,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Array.java b/src/Array.java index e194c0f..83b493f 100644 --- a/src/Array.java +++ b/src/Array.java @@ -1,7 +1,21 @@ +import com.sun.source.tree.BreakTree; + import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.Spliterator; +import java.util.function.Consumer; public class Array implements IntList { + private int[] buffer; + private int size; + private final static int INITIAL_CAPACITY = 10; + + public Array() { + buffer = new int[10]; + size = 0; + + } /** * Prepends (inserts) the specified value at the front of the list (at index 0). @@ -12,16 +26,45 @@ public class Array implements IntList { */ @Override public void addFront(int value) { + if (size == buffer.length) { + resize(2 * buffer.length); + } + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + + + buffer[0] = value; + + + size++; } - /** - * Appends (inserts) the specified value at the back of the list (at index size()-1). - * - * @param value value to be inserted - */ + + private void resize(int newSize) { + + //create new array + int[] temp = new int[newSize]; + // copy values from buffer + for (int i = 0; i < size; i++) { + temp[i] = buffer[i]; + } + //make the switchover + buffer = temp; + } + @Override public void addBack(int value) { + if (size == buffer.length) { + //create new array and copy the old array over + resize(2 * buffer.length); + //making new array twice as large + } + + buffer[size] = value; + size++; + } @@ -55,6 +98,13 @@ public void removeFront() { */ @Override public void removeBack() { + if (size == 0) { + throw new IllegalStateException("already empty"); + } + + size--; + buffer[size] = 0; + } @@ -81,7 +131,7 @@ public int remove(int index) { */ @Override public int get(int index) { - return 0; + return buffer.length; } /** @@ -92,9 +142,13 @@ public int get(int index) { */ @Override public boolean contains(int value) { + for (int i = 0; i < size; i++) { + if (get(i) == value) { + return true; + } + } return false; } - /** * Returns the index of the first occurrence of the specified value * in this list, or -1 if this list does not contain the value. @@ -142,9 +196,88 @@ public void clear() { * * @return an Iterator. */ +// @Override +// public Iterator iterator() { +// return new ArrayIntListIterator(); +// +// +// } + + @Override + public String toString() { + if (size == 0) { + return "[]"; + + } + StringBuilder sb = new StringBuilder(); + sb.append("["); + + sb.append(buffer[0]); + for (int i = 1; i < size; i++) { + + sb.append(", "); + sb.append(buffer[i]); + } + + sb.append("]"); + return sb.toString(); + + } + @Override public Iterator iterator() { return null; } -} + + @Override + public void forEach(Consumer action) { + IntList.super.forEach(action); + } + + @Override + public Spliterator spliterator() { + return IntList.super.spliterator(); + } + + //nested class +// public class ArrayIntListIterator extends Iterator { +// private int currentPosition; +// +// public ArrayIntListIterator() { +// currentPosition = 0; +// +// } +// +// public boolean hasNext() { +// } +// +// @Override +// public Integer next() { +// if (!hasNext()) { +// throw new NoSuchElementException(); +// } +// +// int value = get(currentPosition); +// currentPosition++; +// return value; +// } + +// @Override +// public void remove() { +// Iterator.super.remove(); +// } +// +// @Override +// public void forEachRemaining(Consumer action) { +// Iterator.super.forEachRemaining(action); +// } +// if (currentPosition < size){ +// return true; +// }else { +// return false; +// } +// } + + + } diff --git a/src/ArrayTest.java b/src/ArrayTest.java new file mode 100644 index 0000000..e1c4241 --- /dev/null +++ b/src/ArrayTest.java @@ -0,0 +1,86 @@ +import static org.junit.jupiter.api.Assertions.*; + +class ArrayTest { + + @org.junit.jupiter.api.Test + void addFront() { + } + + @org.junit.jupiter.api.Test + void addBackWithEmptyList() { + Array theList = new Array(); + theList.addBack(42); + assertEquals(theList.get(0), 42); + + } + @org.junit.jupiter.api.Test + void addBackWithNonEmptyList() { + Array theList = new Array(); + theList.addBack(1); + theList.addBack(42); + assertEquals(theList.get(0), 42); + + } + @org.junit.jupiter.api.Test + void addBackWithResize() { + Array theList = new Array(); + for (int i = 0; i < 10; i++) { + theList.addBack(i); + + + } + theList.addBack(42); + assertEquals(theList.get(theList.size()-1), 42); + } + + @org.junit.jupiter.api.Test + void add() { + } + + @org.junit.jupiter.api.Test + void removeFront() { + } + + @org.junit.jupiter.api.Test + void removeBackFromEmptyList() { + Array theList = new Array(); + assertThrowsExactly(IllegalStateException.class, + theList::removeBack); + } + + @org.junit.jupiter.api.Test + void remove() { + } + + @org.junit.jupiter.api.Test + void get() { + } + + @org.junit.jupiter.api.Test + void contains() { + } + + @org.junit.jupiter.api.Test + void indexOf() { + } + + @org.junit.jupiter.api.Test + void isEmpty() { + } + + @org.junit.jupiter.api.Test + void size() { + } + + @org.junit.jupiter.api.Test + void clear() { + } + + @org.junit.jupiter.api.Test + void iterator() { + } + + @org.junit.jupiter.api.Test + void testToString() { + } +} \ No newline at end of file diff --git a/src/Linkedintlist.java b/src/Linkedintlist.java index 27bca2c..17cf84b 100644 --- a/src/Linkedintlist.java +++ b/src/Linkedintlist.java @@ -1,147 +1,189 @@ -import java.util.Iterator; - -public class Linkedintlist implements IntList { - /** - * Prepends (inserts) the specified value at the front of the list (at index 0). - * Shifts the value currently at the front of the list (if any) and any - * subsequent values to the right. - * - * @param value value to be inserted - */ - @Override - public void addFront(int value) { - - } - - /** - * Appends (inserts) the specified value at the back of the list (at index size()-1). - * - * @param value value to be inserted - */ - @Override - public void addBack(int value) { - - } - - /** - * Inserts the specified value at the specified position in this list. - * Shifts the value currently at that position (if any) and any subsequent - * values to the right. - * - * @param index index at which the specified value is to be inserted - * @param value value to be inserted - * @throws IndexOutOfBoundsException if the index is out of range - */ - @Override - public void add(int index, int value) { - - } - - /** - * Removes the value located at the front of the list - * (at index 0), if it is present. - * Shifts any subsequent values to the left. - */ - @Override - public void removeFront() { - - } - - /** - * Removes the value located at the back of the list - * (at index size()-1), if it is present. - */ - @Override - public void removeBack() { - - } - - /** - * Removes the value at the specified position in this list. - * Shifts any subsequent values to the left. Returns the value - * that was removed from the list. - * - * @param index the index of the value to be removed - * @return the value previously at the specified position - * @throws IndexOutOfBoundsException if the index is out of range - */ - @Override - public int remove(int index) { - return 0; - } - - /** - * Returns the value at the specified position in the list. - * - * @param index index of the value to return - * @return the value at the specified position in this list - * @throws IndexOutOfBoundsException if the index is out of range - */ - @Override - public int get(int index) { - return 0; - } - - /** - * Returns true if this list contains the specified value. - * - * @param value value whose presence in this list is to be searched for - * @return true if this list contains the specified value - */ - @Override - public boolean contains(int value) { - return false; - } - - /** - * Returns the index of the first occurrence of the specified value - * in this list, or -1 if this list does not contain the value. - * - * @param value value to search for - * @return the index of the first occurrence of the specified value in this list - * or -1 if this list does not contain the value - */ - @Override - public int indexOf(int value) { - return 0; - } - - /** - * Returns true if this list contains no values. - * - * @return true if this list contains no values - */ - @Override - public boolean isEmpty() { - return false; - } - - /** - * Returns the number of values in this list. - * - * @return the number of values in this list - */ - @Override - public int size() { - return 0; - } - - /** - * Removes all the values from this list. - * The list will be empty after this call returns. - */ - @Override - public void clear() { - - } - - /** - * Returns an iterator over elements of type {@code T}. - * - * @return an Iterator. - */ - @Override - public Iterator iterator() { - return null; - } -} +//import org.w3c.dom.Node; +// +//import java.util.Iterator; +// +//public class Linkedintlist implements IntList { +// /** +// * Prepends (inserts) the specified value at the front of the list (at index 0). +// * Shifts the value currently at the front of the list (if any) and any +// * subsequent values to the right. +// * +// * @param value value to be inserte +// +// */ +// public Node() +// +// +// +// private Node head; +// priate int size; +// +//// removefront +//// addfront +//// addback +//// removeback +//// +// +// +// +// +// void removebackemptylist +// +// +// +// public linked list; +// +// @Override +// +// public void addFront(int value) { +// //head = new Node(value, null); +// //if list is empty +// if(head == null){ +// head = new Node(value, null); +// +// +// } +// head = new Node(value,head ); +// else { +// +// +// } +// +// size++; +// //if list if not empty +// +// +// } +// +// /** +// * Appends (inserts) the specified value at the back of the list (at index size()-1). +// * +// * @param value value to be inserted +// */ +// @Override +// public void addBack(int value) { +// +// } +// +// /** +// * Inserts the specified value at the specified position in this list. +// * Shifts the value currently at that position (if any) and any subsequent +// * values to the right. +// * +// * @param index index at which the specified value is to be inserted +// * @param value value to be inserted +// * @throws IndexOutOfBoundsException if the index is out of range +// */ +// @Override +// public void add(int index, int value) { +// +// } +// +// /** +// * Removes the value located at the front of the list +// * (at index 0), if it is present. +// * Shifts any subsequent values to the left. +// */ +// @Override +// public void removeFront() { +// +// } +// +// /** +// * Removes the value located at the back of the list +// * (at index size()-1), if it is present. +// */ +// @Override +// public void removeBack() { +// +// } +// +// /** +// * Removes the value at the specified position in this list. +// * Shifts any subsequent values to the left. Returns the value +// * that was removed from the list. +// * +// * @param index the index of the value to be removed +// * @return the value previously at the specified position +// * @throws IndexOutOfBoundsException if the index is out of range +// */ +// @Override +// public int remove(int index) { +// return 0; +// } +// +// /** +// * Returns the value at the specified position in the list. +// * +// * @param index index of the value to return +// * @return the value at the specified position in this list +// * @throws IndexOutOfBoundsException if the index is out of range +// */ +// @Override +// public int get(int index) { +// return 0; +// } +// +// /** +// * Returns true if this list contains the specified value. +// * +// * @param value value whose presence in this list is to be searched for +// * @return true if this list contains the specified value +// */ +// @Override +// public boolean contains(int value) { +// return false; +// } +// +// /** +// * Returns the index of the first occurrence of the specified value +// * in this list, or -1 if this list does not contain the value. +// * +// * @param value value to search for +// * @return the index of the first occurrence of the specified value in this list +// * or -1 if this list does not contain the value +// */ +// @Override +// public int indexOf(int value) { +// return 0; +// } +// +// /** +// * Returns true if this list contains no values. +// * +// * @return true if this list contains no values +// */ +// @Override +// public boolean isEmpty() { +// return false; +// } +// +// /** +// * Returns the number of values in this list. +// * +// * @return the number of values in this list +// */ +// @Override +// public int size() { +// return 0; +// } +// +// /** +// * Removes all the values from this list. +// * The list will be empty after this call returns. +// */ +// @Override +// public void clear() { +// +// } +// +// /** +// * Returns an iterator over elements of type {@code T}. +// * +// * @return an Iterator. +// */ +// @Override +// public Iterator iterator() { +// return null; +// } +//} diff --git a/src/Main.java b/src/Main.java index cafd36a..df059ef 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,14 +1,36 @@ +import java.util.Iterator; + //TIP To Run code, press or // click the icon in the gutter. public class Main { public static void main(String[] args) { + + int [] arrayofNumbers = new int[10]; + IntList list1 = new Array(); - IntList list2 = new Linkedintlist(); +// IntList list2 = new Linkedintlist(); list1.addBack(42); list1.addBack(82); list1.addBack(97); System.out.println(list1); + System.out.println(list1.contains(97)); + + + +// for(int value : list1){ +// System.out.println(value); +// +// } +// System.out.println("----------"); +// Iterator itr = list1.iterator(); +// while(itr.hasNext()){ +// int value= itr.next(); +// System.out.println(value); + } } -} \ No newline at end of file + + + + From 7e686b40812a9650549be2a274ce7d2d3d8a9fb9 Mon Sep 17 00:00:00 2001 From: cmatthews444 <155667279+cmatthews444@users.noreply.github.com> Date: Mon, 18 Mar 2024 22:24:16 -0700 Subject: [PATCH 3/3] Finishedish --- src/Array.java | 182 +++++++++++++++++++++------------------ src/Linkedintlist.java | 189 ----------------------------------------- src/Main.java | 6 +- 3 files changed, 100 insertions(+), 277 deletions(-) delete mode 100644 src/Linkedintlist.java diff --git a/src/Array.java b/src/Array.java index 83b493f..85314ef 100644 --- a/src/Array.java +++ b/src/Array.java @@ -57,9 +57,9 @@ private void resize(int newSize) { @Override public void addBack(int value) { if (size == buffer.length) { - //create new array and copy the old array over + //create new array and uses buffer to increase size resize(2 * buffer.length); - //making new array twice as large + } buffer[size] = value; @@ -79,9 +79,23 @@ public void addBack(int value) { */ @Override public void add(int index, int value) { + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("This index does not exist"); + } + if (size == buffer.length) { + resize(buffer.length * 2); + } + + for (int i = size; i > index; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[index] = value; + size++; } + /** * Removes the value located at the front of the list * (at index 0), if it is present. @@ -89,9 +103,15 @@ public void add(int index, int value) { */ @Override public void removeFront() { - + // Move everything to the left + for (int i = 0; i < size; i++) { + buffer[i] = buffer[i + 1]; + } + // take away front + size--; } + /** * Removes the value located at the back of the list * (at index size()-1), if it is present. @@ -119,7 +139,19 @@ public void removeBack() { */ @Override public int remove(int index) { - return 0; + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("No index"); + } + int remove = buffer[index]; + + for (int i = index; i < size; i++) { + //shifting left + buffer[i] = buffer[i + 1]; + } + + size--; + + return remove; } /** @@ -131,9 +163,20 @@ public int remove(int index) { */ @Override public int get(int index) { - return buffer.length; + if (index > size || index < 0) { + throw new IndexOutOfBoundsException("This index doesint exist"); + } + + for (int i = 0; i <= index; i++) { + if (i == index) { + return buffer[index]; + } + } + + return -1; } + /** * Returns true if this list contains the specified value. * @@ -143,12 +186,13 @@ public int get(int index) { @Override public boolean contains(int value) { for (int i = 0; i < size; i++) { - if (get(i) == value) { - return true; + if (get(i) != value) { + return false; } } - return false; + return true; } + /** * Returns the index of the first occurrence of the specified value * in this list, or -1 if this list does not contain the value. @@ -159,7 +203,13 @@ public boolean contains(int value) { */ @Override public int indexOf(int value) { - return 0; + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; + } + } + // return -1, I dont really understand that standard... + return -1; } /** @@ -169,17 +219,24 @@ public int indexOf(int value) { */ @Override public boolean isEmpty() { - return false; + if (size != 0) { + return false; + } + // not 0 not empty + return true; } /** * Returns the number of values in this list. + *

+ * Speed: O(1) + * just returns a value * * @return the number of values in this list */ @Override public int size() { - return 0; + return size; } /** @@ -188,40 +245,8 @@ public int size() { */ @Override public void clear() { - - } - - /** - * Returns an iterator over elements of type {@code T}. - * - * @return an Iterator. - */ -// @Override -// public Iterator iterator() { -// return new ArrayIntListIterator(); -// -// -// } - - @Override - public String toString() { - if (size == 0) { - return "[]"; - - } - StringBuilder sb = new StringBuilder(); - sb.append("["); - - sb.append(buffer[0]); - for (int i = 1; i < size; i++) { - - sb.append(", "); - sb.append(buffer[i]); - } - - sb.append("]"); - return sb.toString(); - + // new empty list + buffer = new int[INITIAL_CAPACITY]; } @Override @@ -239,45 +264,34 @@ public Spliterator spliterator() { return IntList.super.spliterator(); } - //nested class -// public class ArrayIntListIterator extends Iterator { -// private int currentPosition; -// -// public ArrayIntListIterator() { -// currentPosition = 0; -// -// } -// -// public boolean hasNext() { -// } -// -// @Override -// public Integer next() { -// if (!hasNext()) { -// throw new NoSuchElementException(); -// } -// -// int value = get(currentPosition); -// currentPosition++; -// return value; -// } - -// @Override -// public void remove() { -// Iterator.super.remove(); -// } -// -// @Override -// public void forEachRemaining(Consumer action) { -// Iterator.super.forEachRemaining(action); -// } -// if (currentPosition < size){ -// return true; -// }else { -// return false; -// } -// } + public class ArrayIntListIterator implements Iterator { + private int Position; - } + // constructor + public ArrayIntListIterator(){ + Position = 0; + } + + @Override + public boolean hasNext() { + if (Position < size()){ + return true; + } else { + return false; + } + + } + + @Override + public Integer next() { + if (!hasNext()){ + throw new NoSuchElementException("You have the end"); + } + int value = get(Position); + Position++; + return value; + } + } +} \ No newline at end of file diff --git a/src/Linkedintlist.java b/src/Linkedintlist.java deleted file mode 100644 index 17cf84b..0000000 --- a/src/Linkedintlist.java +++ /dev/null @@ -1,189 +0,0 @@ -//import org.w3c.dom.Node; -// -//import java.util.Iterator; -// -//public class Linkedintlist implements IntList { -// /** -// * Prepends (inserts) the specified value at the front of the list (at index 0). -// * Shifts the value currently at the front of the list (if any) and any -// * subsequent values to the right. -// * -// * @param value value to be inserte -// -// */ -// public Node() -// -// -// -// private Node head; -// priate int size; -// -//// removefront -//// addfront -//// addback -//// removeback -//// -// -// -// -// -// void removebackemptylist -// -// -// -// public linked list; -// -// @Override -// -// public void addFront(int value) { -// //head = new Node(value, null); -// //if list is empty -// if(head == null){ -// head = new Node(value, null); -// -// -// } -// head = new Node(value,head ); -// else { -// -// -// } -// -// size++; -// //if list if not empty -// -// -// } -// -// /** -// * Appends (inserts) the specified value at the back of the list (at index size()-1). -// * -// * @param value value to be inserted -// */ -// @Override -// public void addBack(int value) { -// -// } -// -// /** -// * Inserts the specified value at the specified position in this list. -// * Shifts the value currently at that position (if any) and any subsequent -// * values to the right. -// * -// * @param index index at which the specified value is to be inserted -// * @param value value to be inserted -// * @throws IndexOutOfBoundsException if the index is out of range -// */ -// @Override -// public void add(int index, int value) { -// -// } -// -// /** -// * Removes the value located at the front of the list -// * (at index 0), if it is present. -// * Shifts any subsequent values to the left. -// */ -// @Override -// public void removeFront() { -// -// } -// -// /** -// * Removes the value located at the back of the list -// * (at index size()-1), if it is present. -// */ -// @Override -// public void removeBack() { -// -// } -// -// /** -// * Removes the value at the specified position in this list. -// * Shifts any subsequent values to the left. Returns the value -// * that was removed from the list. -// * -// * @param index the index of the value to be removed -// * @return the value previously at the specified position -// * @throws IndexOutOfBoundsException if the index is out of range -// */ -// @Override -// public int remove(int index) { -// return 0; -// } -// -// /** -// * Returns the value at the specified position in the list. -// * -// * @param index index of the value to return -// * @return the value at the specified position in this list -// * @throws IndexOutOfBoundsException if the index is out of range -// */ -// @Override -// public int get(int index) { -// return 0; -// } -// -// /** -// * Returns true if this list contains the specified value. -// * -// * @param value value whose presence in this list is to be searched for -// * @return true if this list contains the specified value -// */ -// @Override -// public boolean contains(int value) { -// return false; -// } -// -// /** -// * Returns the index of the first occurrence of the specified value -// * in this list, or -1 if this list does not contain the value. -// * -// * @param value value to search for -// * @return the index of the first occurrence of the specified value in this list -// * or -1 if this list does not contain the value -// */ -// @Override -// public int indexOf(int value) { -// return 0; -// } -// -// /** -// * Returns true if this list contains no values. -// * -// * @return true if this list contains no values -// */ -// @Override -// public boolean isEmpty() { -// return false; -// } -// -// /** -// * Returns the number of values in this list. -// * -// * @return the number of values in this list -// */ -// @Override -// public int size() { -// return 0; -// } -// -// /** -// * Removes all the values from this list. -// * The list will be empty after this call returns. -// */ -// @Override -// public void clear() { -// -// } -// -// /** -// * Returns an iterator over elements of type {@code T}. -// * -// * @return an Iterator. -// */ -// @Override -// public Iterator iterator() { -// return null; -// } -//} diff --git a/src/Main.java b/src/Main.java index df059ef..2a8a2a8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,19 +1,17 @@ import java.util.Iterator; -//TIP To Run code, press or -// click the icon in the gutter. public class Main { public static void main(String[] args) { int [] arrayofNumbers = new int[10]; IntList list1 = new Array(); -// IntList list2 = new Linkedintlist(); + IntList list2 = new Linkedintlist(); list1.addBack(42); list1.addBack(82); list1.addBack(97); - +list1.add(10,5); System.out.println(list1); System.out.println(list1.contains(97));