E - the type of the elementspublic class ArrayList<E>
extends java.lang.Object
java.util.ArrayList in order to
avoid dependencies to the collections framework.| Modifier and Type | Class and Description |
|---|---|
private class |
ArrayList.Itr
Implements an iterator.
|
| Modifier and Type | Field and Description |
|---|---|
private java.lang.Object[] |
elementData
The array buffer into which the elements of the ArrayList are stored.
|
private int |
modCount
The number of times this list has been structurally modified.
|
private int |
size
The size of the ArrayList (the number of elements it contains).
|
| Constructor and Description |
|---|
ArrayList()
Constructs an empty list with an initial capacity of ten.
|
ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E element)
Appends the specified element to the end of this list.
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this
list.
|
void |
clear()
Removes all of the elements from this list.
|
boolean |
contains(java.lang.Object object)
Returns true if this list contains the specified element.
|
static <T> T[] |
copyOf(T[] original,
int newLength)
Copies the specified array, truncating or padding with
nulls (if necessary)
so the copy has the specified length.
|
static <T,U> T[] |
copyOf(U[] original,
int newLength,
java.lang.Class<? extends T[]> newType)
Copies the specified array, truncating or padding with nulls
(if necessary)
so the copy has the specified length.
|
void |
ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList instance, if
necessary, to ensure that it can hold at least the number of elements
specified by the minimum capacity argument.
|
private void |
fastRemove(int index)
Remove method that skips bounds checking and does not
return the value removed.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
int |
indexOf(java.lang.Object object)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
boolean |
isEmpty()
Returns true if this list contains no elements.
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
|
private void |
rangeCheck(int index)
Checks if the given index is in range.
|
boolean |
remove(E element)
Removes the first occurrence of the specified element from this list,
if it is present.
|
E |
remove(int index)
Removes the element at the specified position in this list.
|
int |
size()
Returns the number of elements in this list.
|
<T> T[] |
toArray(T[] array)
Returns an array containing all of the elements in this list in proper
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
|
java.lang.String |
toString()
Returns a textual representation of this list.
|
private transient java.lang.Object[] elementData
private int size
private transient int modCount
public ArrayList(int initialCapacity)
initialCapacity - the initial capacity of the listjava.lang.IllegalArgumentException - if the specified initial capacity
is negativepublic ArrayList()
public boolean add(E element)
element - element to be appended to this listpublic void add(int index,
E element)
index - index at which the specified element is to be insertedelement - element to be insertedjava.lang.IndexOutOfBoundsException - if index<0 || index>=size()public void ensureCapacity(int minCapacity)
minCapacity - the desired minimum capacitypublic E get(int index)
index - index of the element to returnjava.lang.IndexOutOfBoundsException - if index < 0 || index >=size()private void rangeCheck(int index)
index - the index to be checkedpublic int size()
public boolean isEmpty()
public E remove(int index)
index - the index of the element to be removedjava.lang.IndexOutOfBoundsException - if index < 0 || index >=size()public <T> T[] toArray(T[] array)
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
T - the type of the array elementsarray - the array into which the elements of the list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.java.lang.ArrayStoreException - if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listjava.lang.NullPointerException - if the specified array is nullpublic static <T> T[] copyOf(T[] original,
int newLength)
T - the element typeoriginal - the array to be copiednewLength - the length of the copy to be returnedjava.lang.NegativeArraySizeException - if newLength is negativejava.lang.NullPointerException - if original is nullpublic static <T,U> T[] copyOf(U[] original,
int newLength,
java.lang.Class<? extends T[]> newType)
U - the element type of the originalT - the element type of the targetoriginal - the array to be copiednewLength - the length of the copy to be returnednewType - the class of the copy to be returnedjava.lang.NegativeArraySizeException - if newLength is negativejava.lang.NullPointerException - if original is nulljava.lang.ArrayStoreException - if an element copied from
original is not of a runtime type that can be stored in
an array of class newTypepublic boolean contains(java.lang.Object object)
object - element whose presence in this list is to be testedpublic int indexOf(java.lang.Object object)
object - the object to search forobject or -1public java.util.Iterator<E> iterator()
modCountpublic boolean remove(E element)
element - element to be removed from this list, if presentprivate void fastRemove(int index)
index - the index of the element to removepublic java.lang.String toString()
toString in class java.lang.Objectpublic void clear()