APCS Java Subset

ap.java.util
Class ArrayList

java.lang.Object
  extended byap.java.util.ArrayList
All Implemented Interfaces:
java.util.Collection, List, java.util.RandomAccess

public class ArrayList
extends java.lang.Object
implements List, java.util.RandomAccess, java.util.Collection

Resizable array-like collection that implements the List interface. Note: for the APCS A course students do not need to know that ArrayList implements the List interface. In the A course, the ArrayList class is a growable array that stores objects (not primitive types). The first valid position for an ArrayList object list is the index zero/0, the last valid position is list.size()-1.

The methods size, iterator listIterator, get and set methods run in constant time (that is the time it takes them to execute is independent of the number of items stored in the ArrayList.) The single-parameter method add runs in amortized constant time. This means it takes O(n) time to add n elements. The method remove and the two-parameter method add. run in linear time since they both require shifting elements.


Constructor Summary
ArrayList()
          Constructs an empty ArrayList.
 
Method Summary
 void add(int index, java.lang.Object o)
          Inserts the argument at the specified position in this list.
 boolean add(java.lang.Object o)
          Append (adds) the argument to the end of this ArrayList.
 java.lang.Object get(int index)
          Returns the element at the specified position in this list.
 Iterator iterator()
          Returns an iterator over the elements in this list (AB only).
 ListIterator listIterator()
          Returns a list iterator over the elements in this list (AB only).
 java.lang.Object remove(int index)
          Removes the element at the specified position from this list.
 java.lang.Object set(int index, java.lang.Object o)
          Replaces the element at the specified position in this list with the specified object.
 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
 
Methods inherited from interface java.util.Collection
addAll, clear, contains, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray
 

Constructor Detail

ArrayList

public ArrayList()
Constructs an empty ArrayList.

Method Detail

add

public boolean add(java.lang.Object o)
Append (adds) the argument to the end of this ArrayList.

Specified by:
add in interface List
Parameters:
o - is the element appended to this list.
Returns:
true as required by the java.util.Collection interface

add

public void add(int index,
                java.lang.Object o)
Inserts the argument at the specified position in this list. Shifts the elements at the specified position and higher to the right (adds one to their indices).

Parameters:
index - is the position at which the new element is added
o - is the new element added to this list
Throws:
java.lang.IndexOutOfBoundsException - if index < 0 || index >= size()

size

public int size()
Returns the number of elements in this list.

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

iterator

public Iterator iterator()
Returns an iterator over the elements in this list (AB only). The iterator returns, via next(), first the element with index 0 and last the element with index size()-1.

Specified by:
iterator in interface List
Returns:
an iterator over the elements in this list

listIterator

public ListIterator listIterator()
Returns a list iterator over the elements in this list (AB only). The iterator returns, via next() first the element with index 0 and last the element with index size()-1.

Specified by:
listIterator in interface List
Returns:
an list iterator over the elements in this list.

get

public java.lang.Object get(int index)
Returns the element at the specified position in this list.

Specified by:
get in interface List
Parameters:
index - is the position of the element returned
Returns:
the element in this list whose position is the argument
Throws:
java.lang.IndexOutOfBoundsException - if index < 0 || index >= size()

set

public java.lang.Object set(int index,
                            java.lang.Object o)
Replaces the element at the specified position in this list with the specified object.

Specified by:
set in interface List
Parameters:
index - is the position of the element that will be replaced
o - is the object to be stored at the specified position
Returns:
the element previously stored at the specified position
Throws:
java.lang.IndexOutOfBoundsException - if index < 0 || index >= size()

remove

public java.lang.Object remove(int index)
Removes the element at the specified position from this list. All elements to the right of the specified position are shifted left (1 is subtracted from their indices) and the size of this list is one less than before the method is called.

Parameters:
index - is the position of the element removed
Returns:
the element previously stored at the specified position
Throws:
java.lang.IndexOutOfBoundsException - if index < 0 || index >= size()

unofficial documentation for the APCS Java Subset