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 new file mode 100644 index 0000000..85314ef --- /dev/null +++ b/src/Array.java @@ -0,0 +1,297 @@ +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). + * 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) { + if (size == buffer.length) { + resize(2 * buffer.length); + } + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + + + buffer[0] = value; + + + size++; + + } + + + 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 uses buffer to increase size + resize(2 * buffer.length); + + } + + buffer[size] = value; + size++; + + + } + + /** + * 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) { + 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. + * Shifts any subsequent values to the left. + */ + @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. + */ + @Override + public void removeBack() { + if (size == 0) { + throw new IllegalStateException("already empty"); + } + + size--; + buffer[size] = 0; + + + } + + /** + * 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) { + 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; + } + + /** + * 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) { + 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. + * + * @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) { + for (int i = 0; i < size; i++) { + if (get(i) != value) { + 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. + * + * @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) { + for (int i = 0; i < size; i++) { + if (buffer[i] == value) { + return i; + } + } + // return -1, I dont really understand that standard... + return -1; + } + + /** + * Returns true if this list contains no values. + * + * @return true if this list contains no values + */ + @Override + public boolean isEmpty() { + 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 size; + } + + /** + * Removes all the values from this list. + * The list will be empty after this call returns. + */ + @Override + public void clear() { + // new empty list + buffer = new int[INITIAL_CAPACITY]; + } + + @Override + public Iterator iterator() { + return null; + } + + @Override + public void forEach(Consumer action) { + IntList.super.forEach(action); + } + + @Override + public Spliterator spliterator() { + return IntList.super.spliterator(); + } + + + 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/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/Main.java b/src/Main.java index 930198c..2a8a2a8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,15 +1,34 @@ -//TIP To Run code, press or -// click the icon in the gutter. +import java.util.Iterator; + 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!"); - - 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); + + int [] arrayofNumbers = new int[10]; + + IntList list1 = new Array(); + 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)); + + + +// 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 + + + +