Skip to content
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
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;

public class ArrayIntList implements IntList{

//fields

private int size;

private int[] buffer;

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

/**
* 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) {
for (int i = size; i >= 1; i--) { // count 5, 4, 3, 2, 1
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
*/
@Override
public void addBack(int value) {
//TODO check to see if we are full - if so, we need to create a larger buffer.

if (size == buffer.length) {
resize(buffer.length * 2);
}


buffer[size + 1] = 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) {

}

/**
* 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) {
//first check index if its valid
if (index < 0) {
throw new IndexOutOfBoundsException("Index cannot be negative");

}
else if (index >= size) {
throw new IndexOutOfBoundsException("Index out of range");
}

int copyOfRemovedValue = buffer[index];

for (int i = index; i < size - 1; i++) {
buffer[i] = buffer[i + 1];
}
buffer[size - 1] = 0;
size--;

return copyOfRemovedValue;
}

/**
* 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() {
buffer = new int[10];
size = 0;
}

public void resize(int newSize) {
int[] newBuffer = new int[newSize];
for (int i = 0; i < size; i++) {
newBuffer[i] = buffer[i];
}
buffer = newBuffer;
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Integer> iterator() {
return null;
}

}

Loading