public class ArrayList extends AbstractList implements List, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable, Rollbackable
Each list has a capacity, and as the array reaches that capacity it is automatically transferred to a larger array. You also have access to ensureCapacity and trimToSize to control the backing array's size, avoiding reallocation or wasted memory.
ArrayList is not synchronized, so if you need multi-threaded access,
consider using:
List l = Collections.synchronizedList(new ArrayList(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a{
Collection
,
List
,
LinkedList
,
Vector
,
Collections.synchronizedList(List)
,
AbstractList
,
Serialized Form$CHECKPOINT, $RECORD$$CHECKPOINT
Constructor and Description |
---|
ArrayList()
Construct a new ArrayList with the default capacity (16).
|
ArrayList(Collection c)
Construct a new ArrayList, and initialize it with the elements
in the supplied Collection.
|
ArrayList(int capacity)
Construct a new ArrayList with the supplied initial capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
void |
add(int index,
java.lang.Object e)
Adds the supplied element at the specified index, shifting all
elements currently at that index or higher one to the right.
|
boolean |
add(java.lang.Object e)
Appends the supplied element to the end of this list.
|
boolean |
addAll(Collection c)
Add each element in the supplied Collection to this List.
|
boolean |
addAll(int index,
Collection c)
Add all elements in the supplied collection, inserting them beginning
at the specified index.
|
void |
clear()
Removes all elements from this List
|
java.lang.Object |
clone()
Creates a shallow copy of this ArrayList (elements are not cloned).
|
boolean |
contains(java.lang.Object e)
Returns true iff element is in this ArrayList.
|
void |
ensureCapacity(int minCapacity)
Guarantees that this list will have at least enough capacity to
hold minCapacity elements.
|
java.lang.Object |
get(int index)
Retrieves the element at the user-supplied index.
|
int |
indexOf(java.lang.Object e)
Returns the lowest index at which element appears in this List, or
-1 if it does not appear.
|
boolean |
isEmpty()
Checks if the list is empty.
|
int |
lastIndexOf(java.lang.Object e)
Returns the highest index at which element appears in this List, or
-1 if it does not appear.
|
java.lang.Object |
remove(int index)
Removes the element at the user-supplied index.
|
protected void |
removeRange(int fromIndex,
int toIndex)
Removes all elements in the half-open interval [fromIndex, toIndex).
|
java.lang.Object |
set(int index,
java.lang.Object e)
Sets the element at the specified index.
|
int |
size()
Returns the number of elements in this list.
|
java.lang.Object[] |
toArray()
Returns an Object array containing all of the elements in this ArrayList.
|
java.lang.Object[] |
toArray(java.lang.Object[] a)
Returns an Array whose component type is the runtime component type of
the passed-in Array.
|
void |
trimToSize()
Trims the capacity of this List to be equal to its size;
a memory saver.
|
equals, getModCount, hashCode, iterator, listIterator, listIterator, setModCount, subList
$GET$CHECKPOINT, $SET$CHECKPOINT, containsAll, remove, removeAll, retainAll, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
$GET$CHECKPOINT, $SET$CHECKPOINT, containsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
public ArrayList(int capacity)
capacity
- initial capacity of this ArrayListjava.lang.IllegalArgumentException
- if capacity is negativepublic ArrayList()
public ArrayList(Collection c)
c
- the collection whose elements will initialize this listjava.lang.NullPointerException
- if c is nullpublic void trimToSize()
public void ensureCapacity(int minCapacity)
minCapacity
- the minimum guaranteed capacitypublic int size()
size
in interface Collection
size
in interface List
size
in class AbstractCollection
public boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface List
isEmpty
in class AbstractCollection
AbstractCollection.size()
public boolean contains(java.lang.Object e)
contains
in interface Collection
contains
in interface List
contains
in class AbstractCollection
e
- the element whose inclusion in the List is being testedpublic int indexOf(java.lang.Object e)
indexOf
in interface List
indexOf
in class AbstractList
e
- the element whose inclusion in the List is being testedpublic int lastIndexOf(java.lang.Object e)
lastIndexOf
in interface List
lastIndexOf
in class AbstractList
e
- the element whose inclusion in the List is being testedpublic java.lang.Object clone()
clone
in class java.lang.Object
public java.lang.Object[] toArray()
toArray
in interface Collection
toArray
in interface List
toArray
in class AbstractCollection
public java.lang.Object[] toArray(java.lang.Object[] a)
toArray
in interface Collection
toArray
in interface List
toArray
in class AbstractCollection
a
- the passed-in Arrayjava.lang.ArrayStoreException
- if the runtime type of a does not allow
an element in this listjava.lang.NullPointerException
- if a is nullpublic java.lang.Object get(int index)
get
in interface List
get
in class AbstractList
index
- the index of the element we are fetchingjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()public java.lang.Object set(int index, java.lang.Object e)
set
in interface List
set
in class AbstractList
index
- the index at which the element is being sete
- the element to be setjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= 0public boolean add(java.lang.Object e)
add
in interface Collection
add
in interface List
add
in class AbstractList
e
- the element to be appended to this listAbstractList.add(int, Object)
public void add(int index, java.lang.Object e)
add
in interface List
add
in class AbstractList
index
- the index at which the element is being addede
- the item being addedjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public java.lang.Object remove(int index)
remove
in interface List
remove
in class AbstractList
index
- the index of the element to be removedjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()AbstractList.modCount
public void clear()
clear
in interface Collection
clear
in interface List
clear
in class AbstractList
AbstractList.remove(int)
,
AbstractList.removeRange(int, int)
public boolean addAll(Collection c)
addAll
in interface Collection
addAll
in interface List
addAll
in class AbstractCollection
c
- a Collection containing elements to be added to this Listjava.lang.NullPointerException
- if c is nullAbstractCollection.add(Object)
public boolean addAll(int index, Collection c)
addAll
in interface List
addAll
in class AbstractList
index
- the index at which the elements will be insertedc
- the Collection containing the elements to be insertedjava.lang.IndexOutOfBoundsException
- if index < 0 || index > 0java.lang.NullPointerException
- if c is nullAbstractList.add(int, Object)
protected void removeRange(int fromIndex, int toIndex)
removeRange
in class AbstractList
fromIndex
- the first index which will be removedtoIndex
- one greater than the last index which will be removedjava.lang.IndexOutOfBoundsException
- if fromIndex > toIndexpublic void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
$COMMIT
in interface Collection
$COMMIT
in interface List
$COMMIT
in class AbstractList
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
$RESTORE
in interface Collection
$RESTORE
in interface List
$RESTORE
in class AbstractList
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)