- All Implemented Interfaces:
IntCollection,IntList,Serializable,Cloneable,Iterable<Integer>,Collection<Integer>,List<Integer>,RandomAccess
- Direct Known Subclasses:
SortedIntArrayList
public class IntArrayList
extends AbstractList<Integer>
implements IntList, RandomAccess, Cloneable, Serializable
An ArrayList that stores things using
int[] instead of Object[]. null values are not supported.- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int[]The array buffer into which the elements of the ArrayList are stored.protected intThe size of the ArrayList (the number of elements it contains).Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty list with an initial capacity of ten.IntArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.IntArrayList(int[] elements) IntArrayList(Collection<? extends Number> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int o) Appends the specified element to the end of this list.voidadd(int index, int element) Inserts the specified element at the specified position in this list.voidInserts the specified element at the specified position in this list.booleanAppends the specified element to the end of this list.booleanaddAll(int index, Collection<? extends Integer> c) Inserts all of the elements in the specified Collection into this list, starting at the specified position.booleanaddAll(Collection<? extends Integer> 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.voidclear()Removes all of the elements from this list.clone()Returns a shallow copy of thisIntArrayListinstance.booleancontains(int elem) Returnstrueif this list contains the specified element.booleanReturnstrueif this list contains the specified element.voidensureCapacity(int minCapacity) Increases the capacity of thisIntArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.get(int index) Returns the element at the specified position in this list.intgetInt(int index) Returns the element at the specified position in this list.intindexOf(int elem) Searches for the first occurrence of the given argument, testing for equality using theequalsmethod.intSearches for the first occurrence of the given argument, testing for equality using theequalsmethod.booleanisEmpty()Tests if this list has no elements.intlastIndexOf(int elem) Returns the index of the last occurrence of the specified object in this list.intlastIndexOf(Object elem) Returns the index of the last occurrence of the specified object in this list.remove(int index) Removes the element at the specified position in this list.booleanRemoves a single instance of the specified element from this list, if it is present (optional operation).intremoveAtIndex(int index) Removes the element at the specified position in this list.booleanremoveByValue(int value) Removes a single instance of the specified element from this list, if it is present (optional operation).protected voidremoveRange(int fromIndex, int toIndex) Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.intset(int index, int element) Replaces the element at the specified position in this list with the specified element.Replaces the element at the specified position in this list with the specified element.intsize()Returns the number of elements in this list.Object[]toArray()Returns an array containing all of the elements in this list in the correct order.<T> T[]toArray(T[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.int[]Returns an array containing all of the elements in this list in the correct order.voidTrims the capacity of thisIntArrayListinstance to be the list's current size.Methods inherited from class java.util.AbstractList
equals, hashCode, iterator, listIterator, listIterator, subListMethods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, replaceAll, retainAll, sort, spliterator, subList
-
Field Details
-
elementData
protected transient int[] elementDataThe array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer. -
size
protected int sizeThe size of the ArrayList (the number of elements it contains).
-
-
Constructor Details
-
IntArrayList
public IntArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the list.- Throws:
IllegalArgumentException- if the specified initial capacity is negative
-
IntArrayList
public IntArrayList()Constructs an empty list with an initial capacity of ten. -
IntArrayList
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. TheIntArrayListinstance has an initial capacity of 110% the size of the specified collection.- Parameters:
c- the collection whose elements are to be placed into this list.- Throws:
NullPointerException- if the specified collection is null.
-
IntArrayList
public IntArrayList(int[] elements)
-
-
Method Details
-
trimToSize
public void trimToSize()Trims the capacity of thisIntArrayListinstance to be the list's current size. An application can use this operation to minimize the storage of anIntArrayListinstance. -
ensureCapacity
public void ensureCapacity(int minCapacity) Increases the capacity of thisIntArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Parameters:
minCapacity- the desired minimum capacity.
-
size
public int size()Returns the number of elements in this list.- Specified by:
sizein interfaceCollection<Integer>- Specified by:
sizein interfaceList<Integer>- Specified by:
sizein classAbstractCollection<Integer>- Returns:
- the number of elements in this list.
-
isEmpty
public boolean isEmpty()Tests if this list has no elements.- Specified by:
isEmptyin interfaceCollection<Integer>- Specified by:
isEmptyin interfaceList<Integer>- Overrides:
isEmptyin classAbstractCollection<Integer>- Returns:
trueif this list has no elements;falseotherwise.
-
contains
Returnstrueif this list contains the specified element.- Specified by:
containsin interfaceCollection<Integer>- Specified by:
containsin interfaceList<Integer>- Overrides:
containsin classAbstractCollection<Integer>- Parameters:
elem- element whose presence in this List is to be tested.- Returns:
trueif the specified element is present;falseotherwise.
-
contains
public boolean contains(int elem) Returnstrueif this list contains the specified element.- Specified by:
containsin interfaceIntCollection- Parameters:
elem- element whose presence in this List is to be tested.- Returns:
trueif the specified element is present;falseotherwise.
-
indexOf
Searches for the first occurrence of the given argument, testing for equality using theequalsmethod. -
indexOf
public int indexOf(int elem) Searches for the first occurrence of the given argument, testing for equality using theequalsmethod. -
lastIndexOf
Returns the index of the last occurrence of the specified object in this list.- Specified by:
lastIndexOfin interfaceList<Integer>- Overrides:
lastIndexOfin classAbstractList<Integer>- Parameters:
elem- the desired element.- Returns:
- the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
-
lastIndexOf
public int lastIndexOf(int elem) Returns the index of the last occurrence of the specified object in this list.- Specified by:
lastIndexOfin interfaceIntList- Parameters:
elem- the desired element.- Returns:
- the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
-
clone
Returns a shallow copy of thisIntArrayListinstance. (The elements themselves are not copied.)- Overrides:
clonein classObject- Returns:
- a clone of this
IntArrayListinstance.
-
toArray
Returns an array containing all of the elements in this list in the correct order.- Specified by:
toArrayin interfaceCollection<Integer>- Specified by:
toArrayin interfaceList<Integer>- Overrides:
toArrayin classAbstractCollection<Integer>- Returns:
- an array containing all of the elements in this list in the correct order.
-
toArrayInt
public int[] toArrayInt()Returns an array containing all of the elements in this list in the correct order.- Specified by:
toArrayIntin interfaceIntCollection- Returns:
- an array containing all of the elements in this list in the correct order.
-
toArray
public <T> T[] toArray(T[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.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 anynullelements.- Specified by:
toArrayin interfaceCollection<Integer>- Specified by:
toArrayin interfaceList<Integer>- Overrides:
toArrayin classAbstractCollection<Integer>- Parameters:
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.- Returns:
- an array containing the elements of the list.
- Throws:
ArrayStoreException- if the runtime type of a is not a supertype of the runtime type of every element in this list.
-
get
Returns the element at the specified position in this list.- Specified by:
getin interfaceList<Integer>- Specified by:
getin classAbstractList<Integer>- Parameters:
index- index of element to return.- Returns:
- the element at the specified position in this list.
- Throws:
IndexOutOfBoundsException- if index is out of range(index < 0 || index >= size()).
-
getInt
public int getInt(int index) Returns the element at the specified position in this list.- Specified by:
getIntin interfaceIntList- Parameters:
index- index of element to return.- Returns:
- the element at the specified position in this list.
- Throws:
IndexOutOfBoundsException- if index is out of range(index < 0 || index >= size()).
-
set
Replaces the element at the specified position in this list with the specified element.- Specified by:
setin interfaceList<Integer>- Overrides:
setin classAbstractList<Integer>- Parameters:
index- index of element to replace.element- element to be stored at the specified position.- Returns:
- the element previously at the specified position.
- Throws:
IndexOutOfBoundsException- if index out of range(index < 0 || index >= size()).
-
set
public int set(int index, int element) Replaces the element at the specified position in this list with the specified element.- Specified by:
setin interfaceIntList- Parameters:
index- index of element to replace.element- element to be stored at the specified position.- Returns:
- the element previously at the specified position.
- Throws:
IndexOutOfBoundsException- if index out of range(index < 0 || index >= size()).
-
add
Appends the specified element to the end of this list.- Specified by:
addin interfaceCollection<Integer>- Specified by:
addin interfaceList<Integer>- Overrides:
addin classAbstractList<Integer>- Parameters:
o- element to be appended to this list.- Returns:
true(as per the general contract of Collection.add).
-
add
public boolean add(int o) Appends the specified element to the end of this list.- Specified by:
addin interfaceIntCollection- Parameters:
o- element to be appended to this list.- Returns:
true(as per the general contract of Collection.add).
-
add
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).- Specified by:
addin interfaceList<Integer>- Overrides:
addin classAbstractList<Integer>- Parameters:
index- index at which the specified element is to be inserted.element- element to be inserted.- Throws:
IndexOutOfBoundsException- if index is out of range(index < 0 || index > size()).
-
add
public void add(int index, int element) 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).- Specified by:
addin interfaceIntList- Parameters:
index- index at which the specified element is to be inserted.element- element to be inserted.- Throws:
IndexOutOfBoundsException- if index is out of range(index < 0 || index > size()).
-
remove
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).- Specified by:
removein interfaceList<Integer>- Overrides:
removein classAbstractList<Integer>- Parameters:
index- the index of the element to removed.- Returns:
- the element that was removed from the list.
- Throws:
IndexOutOfBoundsException- if index out of range(index < 0 || index >= size()).
-
removeAtIndex
public int removeAtIndex(int index) Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).- Specified by:
removeAtIndexin interfaceIntList- Parameters:
index- the index of the element to removed.- Returns:
- the element that was removed from the list.
- Throws:
IndexOutOfBoundsException- if index out of range(index < 0 || index >= size()).
-
remove
Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an elementesuch that(o == null ? e == null : o.equals(e)), if the list contains one or more such elements. Returnstrueif the list contained the specified element (or equivalently, if the list changed as a result of the call).- Specified by:
removein interfaceCollection<Integer>- Specified by:
removein interfaceList<Integer>- Overrides:
removein classAbstractCollection<Integer>- Parameters:
o- element to be removed from this list, if present.- Returns:
trueif the list contained the specified element.
-
removeByValue
public boolean removeByValue(int value) Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an elementesuch that(o == null ? e == null : o.equals(e)), if the list contains one or more such elements. Returnstrueif the list contained the specified element (or equivalently, if the list changed as a result of the call).- Specified by:
removeByValuein interfaceIntCollection- Parameters:
value- element to be removed from this list, if present.- Returns:
trueif the list contained the specified element.
-
clear
public void clear()Removes all of the elements from this list. The list will be empty after this call returns.- Specified by:
clearin interfaceCollection<Integer>- Specified by:
clearin interfaceList<Integer>- Overrides:
clearin classAbstractList<Integer>
-
addAll
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. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)- Specified by:
addAllin interfaceCollection<Integer>- Specified by:
addAllin interfaceList<Integer>- Overrides:
addAllin classAbstractCollection<Integer>- Parameters:
c- the elements to be inserted into this list.- Returns:
trueif this list changed as a result of the call.- Throws:
NullPointerException- if the specified collection is null.
-
addAll
Inserts all of the elements in the specified Collection into this list, starting at the specified position. Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the specified Collection's iterator.- Specified by:
addAllin interfaceList<Integer>- Overrides:
addAllin classAbstractList<Integer>- Parameters:
index- index at which to insert first element from the specified collection.c- elements to be inserted into this list.- Returns:
trueif this list changed as a result of the call.- Throws:
IndexOutOfBoundsException- if index out of range(index < 0 || index > size()).NullPointerException- if the specified Collection is null.
-
removeRange
protected void removeRange(int fromIndex, int toIndex) Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by(toIndex - fromIndex)elements. (IftoIndex == fromIndex, this operation has no effect.)- Overrides:
removeRangein classAbstractList<Integer>- Parameters:
fromIndex- index of first element to be removed.toIndex- index after last element to be removed.
-
