I am trying to write a removeFirst method for an ArrayList of the generic type. The removeFirst method is supposed to do a few things: 1) If the list is empty it throws an error. 2) It stores the data in a node pointed to by head. 3) Set the head to head's next. 4) decrement the size. 5) returns the data. I can do 1,4, and 5 but I am stuck on 2 and 3. Here is the code I have so far. Please take a look and give me a hint or suggestion:
================================================== =======
public class ArrayList<T> implements ListADT<T>
{
protected final int DEFAULT_CAPACITY = 100;
private final int NOT_FOUND = -1;
protected int rear;
protected T[] list;
//-----------------------------------------------------------------
// Creates an empty list using the default capacity.
//-----------------------------------------------------------------
public ArrayList()
{
rear = 0;
list = (T[])(new Object[DEFAULT_CAPACITY]);
}
//-----------------------------------------------------------------
// Creates an empty list using the specified capacity.
//-----------------------------------------------------------------
public ArrayList (int initialCapacity)
{
rear = 0;
list = (T[])(new Object[initialCapacity]);
}
//-----------------------------------------------------------------
// Removes and returns the first element in the list.
//-----------------------------------------------------------------
public T removeFirst()throws EmptyCollectionException
{
if (rear == 0)
throw new EmptyCollectionException("Trying to remove first element");
return result;
}
//-----------------------------------------------------------------
// Removes and returns the specified element.
//-----------------------------------------------------------------
public T remove (T element)
{
T result;
int index = find (element);
if (index == NOT_FOUND)
throw new ElementNotFoundException ("list");
result = list[index];
rear--;
// shift the appropriate elements
for (int scan=index; scan < rear; scan++)
list[scan] = list[scan+1];
list[rear] = null;
return result;
}
//-----------------------------------------------------------------
// Returns a reference to the element at the front of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.
//-----------------------------------------------------------------
public T first() throws EmptyCollectionException
{ return result;
}
//-----------------------------------------------------------------
// Returns a reference to the element at the rear of the list.
// The element is not removed from the list. Throws an
// EmptyCollectionException if the list is empty.
//-----------------------------------------------------------------
public T last() throws EmptyCollectionException
{ return result;
}
//-----------------------------------------------------------------
// Returns true if this list contains the specified element.
//-----------------------------------------------------------------
public boolean contains (T target)
{
return (find(target) != NOT_FOUND);
}
//-----------------------------------------------------------------
// Returns the array index of the specified element, or the
// constant NOT_FOUND if it is not found.
//-----------------------------------------------------------------
private int find (T target)
{
int scan = 0, result = NOT_FOUND;
boolean found = false;
if (! isEmpty())
while (! found && scan < rear)
if (target.equals(list[scan]))
found = true;
else
scan++;
if (found)
result = scan;
return result;
}
//-----------------------------------------------------------------
// Returns true if this list is empty and false otherwise.
//-----------------------------------------------------------------
public boolean isEmpty()
{
return (rear==0);
}
//-----------------------------------------------------------------
// Returns the number of elements currently in this list.
//-----------------------------------------------------------------
public int size()
{
return rear;
}
//-----------------------------------------------------------------
// Returns an iterator for the elements currently in this list.
//-----------------------------------------------------------------
public Iterator<T> iterator()
{
return new ArrayIterator<T> (list, rear);
}
//-----------------------------------------------------------------
// Returns a string representation of this list.
//-----------------------------------------------------------------
public String toString()
{
String result = " ";
for (int index = 0; index < rear; index++)
result = result + list[index].toString() + "\n";
return result;
}
//-----------------------------------------------------------------
// Creates a new array to store the contents of the list with
// twice the capacity of the old one.
//-----------------------------------------------------------------
protected void expandCapacity()
{
T[] larger = (T[])(new Object[list.length*2]);
for (int scan=0; scan < list.length; scan++)
larger[scan] = list[scan];
list = larger;
}
}