public class DynamicArray<E> extends AbstractList<E> implements List<E>, RandomAccess
Modifier and Type | Field and Description |
---|---|
Object[] |
data
The array buffer into which the elements of the ArrayList are stored.
|
static DynamicArray |
EMPTY |
int |
size
The size of the ArrayList (the number of elements it contains).
|
modCount
Modifier | Constructor and Description |
---|---|
|
DynamicArray()
Constructs an empty list with an demo capacity of ten.
|
|
DynamicArray(Collection<? extends E> c)
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's
iterator.
|
protected |
DynamicArray(DynamicArray<E> source) |
|
DynamicArray(int initialCapacity)
Constructs an empty list with the specified demo capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
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.
|
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of
this list, in the order that they are returned by the
specified collection's Iterator.
|
boolean |
addAll(int index,
Collection<? extends E> c)
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
|
void |
clear()
Removes all of the elements from this list.
|
boolean |
contains(Object o)
Returns true if this list contains the specified element.
|
DynamicArray<E> |
copy()
Returns a shallow copy of this ArrayList instance.
|
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.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
int |
indexOf(Object o)
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.
|
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
E |
remove(int index)
Removes the element at the specified position in this list.
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list,
if it is present.
|
protected void |
removeRange(int fromIndex,
int toIndex)
Removes from this list all of the elements whose index is between
fromIndex, inclusive, and toIndex, exclusive.
|
E |
set(int index,
E element)
Replaces the element at the specified position in this list with
the specified element.
|
int |
size()
Returns the number of elements in this list.
|
Object[] |
toArray()
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] a)
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.
|
void |
trimToSize()
Trims the capacity of this ArrayList instance to be the
list's current size.
|
equals, hashCode, iterator, listIterator, listIterator, subList
containsAll, removeAll, retainAll, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, replaceAll, retainAll, sort, spliterator, subList
parallelStream, removeIf, stream
public static final DynamicArray EMPTY
public transient Object[] data
public int size
public DynamicArray(int initialCapacity)
initialCapacity
- the demo capacity of the listIllegalArgumentException
- if the specified demo capacity
is negativepublic DynamicArray()
public DynamicArray(Collection<? extends E> c)
c
- the collection whose elements are to be placed into this listNullPointerException
- if the specified collection is nullprotected DynamicArray(DynamicArray<E> source)
public DynamicArray<E> copy()
public void trimToSize()
public void ensureCapacity(int minCapacity)
minCapacity
- the desired minimum capacitypublic int size()
size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class AbstractCollection<E>
public boolean contains(Object o)
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
o
- element whose presence in this list is to be testedpublic int indexOf(Object o)
public int lastIndexOf(Object o)
lastIndexOf
in interface List<E>
lastIndexOf
in class AbstractList<E>
public Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
public <T> T[] toArray(T[] a)
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.)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
a
- 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.ArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listNullPointerException
- if the specified array is nullpublic E get(int index)
get
in interface List<E>
get
in class AbstractList<E>
index
- index of the element to returnIndexOutOfBoundsException
public E set(int index, E element)
set
in interface List<E>
set
in class AbstractList<E>
index
- index of the element to replaceelement
- element to be stored at the specified positionIndexOutOfBoundsException
public boolean add(E e)
add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
e
- element to be appended to this listCollection.add(E)
)public void add(int index, E element)
add
in interface List<E>
add
in class AbstractList<E>
index
- index at which the specified element is to be insertedelement
- element to be insertedIndexOutOfBoundsException
public E remove(int index)
remove
in interface List<E>
remove
in class AbstractList<E>
index
- the index of the element to be removedIndexOutOfBoundsException
public boolean remove(Object o)
remove
in interface Collection<E>
remove
in interface List<E>
remove
in class AbstractCollection<E>
o
- element to be removed from this list, if presentpublic void clear()
clear
in interface Collection<E>
clear
in interface List<E>
clear
in class AbstractList<E>
public boolean addAll(Collection<? extends E> c)
addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
c
- collection containing elements to be added to this listNullPointerException
- if the specified collection is nullpublic boolean addAll(int index, Collection<? extends E> c)
addAll
in interface List<E>
addAll
in class AbstractList<E>
index
- index at which to insert the first element from the
specified collectionc
- collection containing elements to be added to this listIndexOutOfBoundsException
NullPointerException
- if the specified collection is nullprotected void removeRange(int fromIndex, int toIndex)
removeRange
in class AbstractList<E>
fromIndex
- index of first element to be removedtoIndex
- index after last element to be removedIndexOutOfBoundsException
- if fromIndex or toIndex out of
range (fromIndex < 0 || fromIndex >= size() || toIndex
> size() || toIndex < fromIndex)Copyright © 2024. All rights reserved.