public class ArrayList<E> implements List<E> {
private static final int defaultMaxSize = 10;
private int maxSize;
private int size;
private int currentPosition;
private E[] array;
/**
* Create a new ArrayList object.
*
* @param maxSize
*/
@SuppressWarnings("unchecked")
public ArrayList(int maxSize) {
this.maxSize = maxSize;
this.size = this.currentPosition = 0;
this.array = (E[]) new Object[this.maxSize];
}
/**
* Create a new ArrayList object.
*/
public ArrayList() {
this(defaultMaxSize);
}
@Override
public void insert(E item) {
assert this.size < this.maxSize : "ArrayList capacity exceeded";
for (int i = this.size; i > this.currentPosition; i--) {
// Shift elements up to make room
this.array[i] = this.array[i - 1];
}
this.array[this.currentPosition] = item;
this.size++;
}
@Override
public void append(E item) {
assert this.size < this.maxSize : "ArrayList capacity exceeded";
this.array[this.size++] = item;
}
@Override
public E remove() {
if ((this.currentPosition < 0) || (this.currentPosition >= this.size)) {
return null;
}
E item = this.array[this.currentPosition];
for (int i = this.currentPosition; i < this.size - 1; i++) {
this.array[i] = this.array[i + 1];
}
this.size--;
return item;
}
@Override
public void clear() {
this.size = this.currentPosition = 0;
}
@Override
public void moveToStart() {
this.currentPosition = 0;
}
@Override
public void moveToEnd() {
this.currentPosition = this.size;
}
@Override
public boolean previous() {
if (this.currentPosition != 0) {
this.currentPosition--;
return true;
} else {
return false;
}
}
@Override
public boolean next() {
if (this.currentPosition < this.size) {
this.currentPosition++;
return true;
} else {
return false;
}
}
@Override
public int length() {
return this.size;
}
@Override
public int currentPosition() {
return this.currentPosition;
}
@Override
public void moveCurrentToPosition(int position) {
assert (position >= 0) && (position <= this.size) : "position is out of range";
this.currentPosition = position;
}
@Override
public E getValue() {
assert (this.currentPosition >= 0) && (this.currentPosition < this.size) :
"No current element";
return this.array[this.currentPosition];
}
}
And here is the List interface!
public interface List<E> {
/**
* Insert an element behind the current position. Must check that the linked
* list's capacity is not exceeded.
*
* @param item
* Item to be inserted.
*/
public void insert(E item);
/**
* Insert an element after the last element in the list.
*
* @param item
* Item to be appended.
*/
public void append(E item);
/**
* Remove the element after the current element and return the value of the
* removed element.
*
* @return The element that was removed.
*/
public E remove();
/**
* Remove all contents from the list.
*/
public void clear();
/**
* Move current position to first element.
*/
public void moveToStart();
/**
* Move current position to last element.
*/
public void moveToEnd();
/**
* Move the current position one element before. No change if already at the
* beginning.
*
* @return True if moved to previous position; otherwise return false.
*/
public boolean previous();
/**
* Move the current position one element after. No change if already at the
* end.
*
* @return True if moved to current position; otherwise return false.
*/
public boolean next();
/**
* @return The number of items in the list.
*/
public int length();
/**
* @return The current position.
*/
public int currentPosition();
/**
* @param position
* Position to move current to.
*/
public void moveCurrentToPosition(int position);
/**
* @return The current item in the current position.
*/
public E getValue();
}