From a81413c4ccdac5967fef659005bc1061e3a091e1 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 26 Aug 2018 11:16:00 -0400 Subject: [PATCH] Created a custom ArrayList with JUnit tests. --- .../Lists/ArrayList/ArrayBackedList.java | 170 ++++++++++++++++++ .../Lists/{LinkedList => }/IList.java | 2 +- .../Lists/LinkedList/LinkedList.java | 1 + .../DataStructures/Lists/Queue/IQueue.java | 2 + .../DataStructures/Lists/ArrayListTest.java | 158 ++++++++++++++++ 5 files changed, 332 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java rename src/main/java/net/jrtechs/www/DataStructures/Lists/{LinkedList => }/IList.java (95%) create mode 100644 src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java diff --git a/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java new file mode 100644 index 0000000..57cf917 --- /dev/null +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java @@ -0,0 +1,170 @@ +package net.jrtechs.www.DataStructures.Lists.ArrayList; + +import net.jrtechs.www.DataStructures.Lists.IList; + + +/** + * Implementation of {@link IList}. This implementation of an + * array list stores data in an array and resizes when necessary + * to fit more data. This dsta structure is intended to + * be used for O(1) access on indexes. Although insertions and + * deletions worst case is O(n),however, the average is closer to O(1) + * since array re-sizes don't commonly occur. + * + * @param generic stored in data structure + * + * @author Jeffery Russell 8-26-18 + */ +public class ArrayBackedList implements IList +{ + /** Default size to increase size of array by */ + private int arraySegmentSize = 20; + + /** How many elements are currently in list */ + private int currentSize = 0; + + /** Array to store data elements in -- + * is an array of Objects since you can't + * create a list of generics*/ + private Object[] dataList; + + + /** + * Creates an empty list + */ + public ArrayBackedList() + { + this.dataList = new Object[this.arraySegmentSize]; + } + + + /** + * Specifies the size at which the array will be resized to + * + * @param size default size of array chunk + */ + public ArrayBackedList(int size) + { + this.arraySegmentSize = size; + this.dataList = new Object[size]; + } + + + /** + * Adds an element to the end of the list + * + * @param o element to get added + * @return whether element was added + */ + @Override + public boolean add(E o) + { + makeArrayLarger(); + this.dataList[currentSize] = o; + this.currentSize++; + return true; + } + + + /** + * Makes the array used to store the elements larger if it is necessary + * for storing more data in the array. + */ + private void makeArrayLarger() + { + if(this.currentSize + 1 > dataList.length) + { + Object[] data = new Object[dataList.length + arraySegmentSize]; + + for(int i = 0; i < this.dataList.length; i++) + data[i] = this.dataList[i]; + this.dataList = data; + } + } + + + /** + * Determines if the list contains a certain element. + * + * @param o element to see if exists + * @return whether element is in list + */ + @Override + public boolean contains(E o) + { + for(int i = 0; i < currentSize; i++) + if(dataList[i].equals(o)) + return true; + return false; + } + + + /** + * Returns the size of the list + * + * @return + */ + @Override + public int size() + { + return this.currentSize; + } + + + /** + * Removes the element at a certain index. + * + * @Warning this can go out of bounds + * + * @param index of element to remove + * @return element removed + */ + @Override + public E remove(int index) + { + Object old = this.dataList[index]; + + for(int i = index; i < this.currentSize -1; i++) + { + this.dataList[i] = this.dataList[i + 1]; + } + this.currentSize--; + return (E)old; + } + + + /** + * Removes an element from an array if it exists. If it does + * not exist in the array, null is returned. + * + * @param o element to remove + * @return element removed + */ + @Override + public E remove(E o) + { + for(int i = 0; i < this.currentSize; i++) + { + if(this.dataList[i].equals(o)) + { + return remove(i); + } + } + return null; + } + + + /** + * Returns the element at a desired index in O(1) time. + * + * @Warning This can go out of bounds with bad input + * + * @param index of desired element + * @return element of a specific index + */ + @Override + public E get(int index) + { + return (E)dataList[index]; + } +} diff --git a/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/IList.java similarity index 95% rename from src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java rename to src/main/java/net/jrtechs/www/DataStructures/Lists/IList.java index d66f5e6..4506a69 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/IList.java @@ -1,4 +1,4 @@ -package net.jrtechs.www.DataStructures.Lists.LinkedList; +package net.jrtechs.www.DataStructures.Lists; /** diff --git a/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java index 12b6982..4e01a14 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java @@ -1,6 +1,7 @@ package net.jrtechs.www.DataStructures.Lists.LinkedList; +import net.jrtechs.www.DataStructures.Lists.IList; import net.jrtechs.www.DataStructures.Node; /** diff --git a/src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java b/src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java index b3b1895..a291a01 100644 --- a/src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java +++ b/src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java @@ -1,6 +1,8 @@ package net.jrtechs.www.DataStructures.Lists.Queue; /** + * Defines behavior for a simple queue + * * @author Jeffery Russell 8-26-18 */ public interface IQueue diff --git a/src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java b/src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java new file mode 100644 index 0000000..f4ffea5 --- /dev/null +++ b/src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java @@ -0,0 +1,158 @@ +package net.jrtechs.www.DataStructures.Lists; + +import net.jrtechs.www.DataStructures.Lists.ArrayList.ArrayBackedList; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * JUnit tests for {@link ArrayBackedList} + * + * @author Jeffery Russell 8-26-18 + */ +public class ArrayListTest +{ + /** + * Makes sure that instantiations work + */ + @Test + public void testCreation() + { + assertNotNull(new ArrayBackedList<>()); + + assertNotNull(new ArrayBackedList(12)); + } + + + /** + * Test Size of list + */ + @Test + public void testSize() + { + ArrayBackedList list = new ArrayBackedList<>(); + list.add(12.0); + assertTrue(list.size() == 1); + list.add(13.0); + assertTrue(list.size() == 2); + } + + + /** + * Testing addition of data elements + */ + @Test + public void testAddition() + { + ArrayBackedList list = new ArrayBackedList<>(); + list.add(12.0); + list.add(13.0); + list.add(44.9); + + assertTrue(list.contains(44.9)); + assertTrue(list.contains(12.0)); + assertTrue(list.contains(13.0)); + + assertTrue(list.size() == 3); + + + ArrayBackedList list2 = new ArrayBackedList<>(20); + list2.add(18.0); + + assertTrue(list2.contains(18.0)); + assertTrue(list2.size() == 1); + } + + + /** + * Testing deletion of data elements + */ + @Test + public void testDeletionByIndex() + { + ArrayBackedList list = new ArrayBackedList<>(); + list.add(12.0); + list.add(13.0); + list.add(44.9); + + assertTrue(list.contains(44.9)); + assertTrue(list.contains(12.0)); + assertTrue(list.contains(13.0)); + + assertTrue(list.size() == 3); + + + list.remove(0); + list.remove(1); + + assertFalse(list.contains(12.0)); + assertFalse(list.contains(44.9)); + assertTrue(list.contains(13.0)); + } + + /** + * Tests linked deletion based on element + */ + @Test + public void testDeletionByElement() + { + ArrayBackedList list = new ArrayBackedList<>(); + list.add(12.0); + list.add(13.0); + list.add(44.9); + + assertTrue(list.contains(44.9)); + assertTrue(list.contains(12.0)); + assertTrue(list.contains(13.0)); + + assertTrue(list.size() == 3); + + + list.remove(12.0); + list.remove(44.9); + + assertFalse(list.contains(12.0)); + assertFalse(list.contains(44.9)); + assertTrue(list.contains(13.0)); + } + + + @Test + public void testArrayResize() + { + ArrayBackedList list = new ArrayBackedList<>(); + + for(int i = 0; i < 3000; i++) + { + list.add(i * 3.14); + } + assertTrue(list.size() == 3000); + + for(int i = 0; i < 3000; i++) + { + list.remove(0); + } + assertTrue(list.size() == 0); + + for(int i = 0; i < 6000; i++) + { + list.add(i * 1.0); + } + assertTrue(list.size() == 6000); + + for(int i = 0; i < 6000; i++) + { + assertTrue(list.contains(i * 1.0)); + list.remove(i* 1.0); + } + + for(int i = 0; i < 6000; i++) + { + assertFalse(list.contains(i * 1.0)); + list.remove(i* 1.0); + } + + } +}