Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eric Boyd IntList #6

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions IntListReview.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,37 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
199 changes: 199 additions & 0 deletions src/ArrayIntList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayIntList implements IntList
{
private int[] buffer;
private int size;
public ArrayIntList()
{
buffer = new int[10];
size = 0;
}

@Override
// Normally fast (constant time unless you have to resize)
public void addBack(int value)
{
checkSize();
buffer[size] = value;
size++;
}

@Override
//Slow must move every element in array O(N)
public void addFront(int value)
{
checkSize();
add(0, value);
}

@Override
//Slow, must move every element in array located above entry index O(N)
public void add(int index, int value)
{
checkSize();
size ++;
checkIndex(index);
for(int i = size; i > index; i--)
{
buffer[i] = buffer[i-1];
}
buffer[index] = value;
}

@Override
// Fast, constant time.
public void removeBack()
{
if(size == 0)
{
throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList.");
}
size--;
}

@Override
//Slow must move every element in array O(N)
public void removeFront()
{
remove(0);
}

@Override
//Slow, must move every element in array located above entry index O(N)
public int remove(int index)
{
if(size == 0)
{
throw new IndexOutOfBoundsException("Attempt to remove from empty arrayList.");
}
checkIndex(index);
int returnInt = buffer[index];
for(int i = index; i < size; i++)
{
buffer[i] = buffer[i + 1];
}
size --;
return returnInt;
}

@Override
//Very fast operation. O(1) constant time
public int get(int index)
{
checkIndex(index);
return buffer[index];
}

@Override
public boolean contains(int value)
{
return indexOf(value) >= 0;
}

@Override
public int indexOf(int value)
{
for (int i = 0; i < size; i++)
{
if(buffer[i] == value) {
return i;
}
}
return -1;
}

@Override
public boolean isEmpty()
{
return size == 0;
}

@Override
public int size()
{
return size;
}

@Override
public void clear()
{
buffer = new int[10];
size = 0;
}
private void checkIndex(int index) throws IndexOutOfBoundsException
{
if(index >= size || index < 0)
{
throw new IndexOutOfBoundsException(index + " is outside Array.");
}
}

public String toString()
{
if(size == 0)
{
return "[]";
}
else
{
StringBuilder returnString = new StringBuilder();
returnString.append("[").append(buffer[0]);
for(int i = 1; i < size; i++)
{
returnString.append(", ").append(buffer[i]);
}
returnString.append("]");
return returnString.toString();
}
}
private void checkSize()
{
if(size > buffer.length - 2)
{
buffer = resize();
}
}
// Slow because you must "touch" every element in array to copy to new array O(N)
private int[] resize() {
int[] returnInt = new int[buffer.length * 2];

for (int i = 0; i < size; i++)
{
returnInt[i] = buffer[i];
}
return returnInt;
}

@Override
public Iterator<Integer> iterator() {
return new ArrayIntListIterator();
}

private class ArrayIntListIterator implements Iterator<Integer>
{
private int position;

public ArrayIntListIterator()
{
position = 0;
}

public boolean hasNext()
{
return position != size;
}

public Integer next()
{
if(hasNext())
{
Integer output = buffer[position];
position++;
return output;
}
throw new NoSuchElementException("No elements left.");
}
}
}
Loading