Browse Source

Created a custom ArrayList with JUnit tests.

master
jrtechs 6 years ago
parent
commit
a81413c4cc
5 changed files with 332 additions and 1 deletions
  1. +170
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java
  2. +1
    -1
      src/main/java/net/jrtechs/www/DataStructures/Lists/IList.java
  3. +1
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java
  4. +2
    -0
      src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java
  5. +158
    -0
      src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java

+ 170
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/ArrayList/ArrayBackedList.java View File

@ -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 <E> generic stored in data structure
*
* @author Jeffery Russell 8-26-18
*/
public class ArrayBackedList<E> implements IList<E>
{
/** 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];
}
}

src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/IList.java → src/main/java/net/jrtechs/www/DataStructures/Lists/IList.java View File

@ -1,4 +1,4 @@
package net.jrtechs.www.DataStructures.Lists.LinkedList;
package net.jrtechs.www.DataStructures.Lists;
/**

+ 1
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/LinkedList/LinkedList.java View File

@ -1,6 +1,7 @@
package net.jrtechs.www.DataStructures.Lists.LinkedList;
import net.jrtechs.www.DataStructures.Lists.IList;
import net.jrtechs.www.DataStructures.Node;
/**

+ 2
- 0
src/main/java/net/jrtechs/www/DataStructures/Lists/Queue/IQueue.java View File

@ -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<E>

+ 158
- 0
src/test/java/net/jrtechs/www/DataStructures/Lists/ArrayListTest.java View File

@ -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<Double>(12));
}
/**
* Test Size of list
*/
@Test
public void testSize()
{
ArrayBackedList<Double> 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<Double> 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<Double> 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<Double> 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<Double> 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<Double> 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);
}
}
}

Loading…
Cancel
Save