Class ListArrayBased

java.lang.Object
  extended byListArrayBased
All Implemented Interfaces:
ListInterface

public class ListArrayBased
extends java.lang.Object
implements ListInterface

Array implementation of the List interface. Permits all elements, including null.

The size, isEmpty, get, and removeAll, operations run in constant time. The add and remove operations run in linear time.

Each ListArrayBased instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size.


Constructor Summary
ListArrayBased()
           
 
Method Summary
 void add(int index, java.lang.Object item)
          Inserts the specified element at the specified position in this list.
 java.lang.Object get(int index)
          Returns the element at the specified position in this list.
 boolean isEmpty()
          Returns true if this list contains no elements.
 void remove(int index)
          Removes the element at the specified position in this list.
 void removeAll()
          Removes from this list all the elements that are contained in the specified collection.
 int size()
          Returns the number of elements in this list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ListArrayBased

public ListArrayBased()
Method Detail

isEmpty

public boolean isEmpty()
Description copied from interface: ListInterface
Returns true if this list contains no elements.

Pre-condition: None
Post-condition: Returns true if the list is empty and false otherwise

Specified by:
isEmpty in interface ListInterface
Returns:
true if this list contains no elements

size

public int size()
Description copied from interface: ListInterface
Returns the number of elements in this list.

Pre-condition: None
Post-condition: Returns the number of elements in the list, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in the list.

Specified by:
size in interface ListInterface
Returns:
the number of elements in this list.

removeAll

public void removeAll()
Description copied from interface: ListInterface
Removes from this list all the elements that are contained in the specified collection.

Pre-condition: None.
Post-condition: The list is empty (this.isEmpty() returns true).

Specified by:
removeAll in interface ListInterface

add

public void add(int index,
                java.lang.Object item)
         throws ListIndexOutOfBoundsException
Description copied from interface: ListInterface
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Pre-condition: index is between 1 and this.size()+1.
Post-condition: item becomes the list element at position index; the position of later elements in the list is increased by 1 to make room for it.

Specified by:
add in interface ListInterface
Parameters:
index - index at which the specified element is to be inserted.
item - element to be inserted.

Throws:
ListIndexOutOfBoundsException - if the index is out of the range 1 up to the size of the list

get

public java.lang.Object get(int index)
                     throws ListIndexOutOfBoundsException
Description copied from interface: ListInterface
Returns the element at the specified position in this list.

Pre-condition: index is between 1 and this.size().
Post-condition: Returns the element at position index.

Specified by:
get in interface ListInterface
Parameters:
index - index of element to return.

Returns:
the element at the specified position in this list

Throws:
ListIndexOutOfBoundsException - if the index is out of the range 1 up to the size of the list

remove

public void remove(int index)
            throws ListIndexOutOfBoundsException
Description copied from interface: ListInterface
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Pre-condition: index is between 1 and this.size()+1.
Post-condition: Removes the element at position index; the position of later elements in the list is decreased by 1.

Specified by:
remove in interface ListInterface
Parameters:
index - index of element to remove.

Throws:
ListIndexOutOfBoundsException - if the index is out of the range 1 up to the size of the list