Skip to content
Buy me a coffee

ArrayList

Defined in: src/list/ArrayList.ts:35

Resizable list backed by a dense native array; the default List implementation.

get and set are O(1), appending is amortized O(1), and inserting or removing at index i is O(n − i) because the tail shifts (so addFirst/removeFirst are O(n)). contains, indexOf and remove(o) are O(n) scans; sort is a stable O(n log n). Permits null and undefined elements. Iterates in index order; iterators, forEach and views are fail-fast, and sort and replaceAll count as structural changes (set does not).

The backing array never contains holes: every index in [0, size) holds a value, so the engine keeps it in a packed elements kind.

E

new ArrayList<E>(): ArrayList<E>

Defined in: src/list/ArrayList.ts:40

Creates an empty list.

ArrayList<E>

AbstractList.constructor

new ArrayList<E>(initialCapacity): ArrayList<E>

Defined in: src/list/ArrayList.ts:47

Creates an empty list. initialCapacity is validated but only a hint: nothing is preallocated, since preallocating would create holes in the backing array.

number

expected number of elements

ArrayList<E>

IllegalArgumentException if initialCapacity is negative or not an integer

AbstractList<E>.constructor

new ArrayList<E>(c): ArrayList<E>

Defined in: src/list/ArrayList.ts:52

Creates a list holding the elements of c, in its iteration order. O(n).

Collection<E>

ArrayList<E>

NullPointerException if c is null or undefined

AbstractList<E>.constructor

modCount: number = 0

Defined in: src/abstract/AbstractList.ts:41

Structural modification counter. Iterators, sublists and spliterators record it and throw ConcurrentModificationException when it changes behind them. Subclasses increment it once per structural change (add, remove, clear); set leaves it alone.

AbstractList.modCount

get [RANDOM_ACCESS](): true

Defined in: src/list/ArrayList.ts:81

Marker for O(1) positional access; always true.

true

[INSPECT](): string

Defined in: src/abstract/AbstractCollection.ts:195

Node’s util.inspect hook: console.log prints the toString() form.

string

AbstractList.[INSPECT]


[iterator](): IterableIterator<E>

Defined in: src/list/ArrayList.ts:423

Returns a JS iterator over the elements in index order, for for...of and spread; fail-fast.

IterableIterator<E>

AbstractList.[iterator]


add(e): boolean

Defined in: src/list/ArrayList.ts:143

Appends e to the end of this list, in amortized O(1). null and undefined are allowed.

E

boolean

always true

AbstractList.add

add(index, e): void

Defined in: src/list/ArrayList.ts:149

Inserts e at index, shifting the element there and all later ones one position to the right. O(n − index).

number

E

void

IndexOutOfBoundsException if index < 0 or index > size()

AbstractList.add


addAll(c): boolean

Defined in: src/abstract/AbstractList.ts:100

Appends the elements of c in its iteration order (via addAllAt); returns true if c was not empty.

Collection<E>

boolean

NullPointerException if c is null

AbstractList.addAll

addAll(index, c): boolean

Defined in: src/abstract/AbstractList.ts:106

Inserts the elements of c at index in its iteration order (via addAllAt); returns true if c was not empty.

number

Collection<E>

boolean

IndexOutOfBoundsException if index < 0 || index > size()

NullPointerException if c is null

AbstractList.addAll


addFirst(e): void

Defined in: src/abstract/AbstractList.ts:237

Inserts e at index 0, via addAtIndex.

E

void

UnsupportedOperationException if addAtIndex is not overridden

AbstractList.addFirst


addLast(e): void

Defined in: src/abstract/AbstractList.ts:245

Appends e at the end, via addAtIndex.

E

void

UnsupportedOperationException if addAtIndex is not overridden

AbstractList.addLast


clear(): void

Defined in: src/list/ArrayList.ts:266

Removes all elements. Truncates the backing array, so no references to the removed elements are kept.

void

AbstractList.clear


clone(): this

Defined in: src/list/ArrayList.ts:100

Returns a shallow, independent copy of the same runtime class, in O(n).

this


contains(o): boolean

Defined in: src/list/ArrayList.ts:200

Returns true if this list contains an element equal to o (by Objects.equals). O(n) scan; null is allowed.

unknown

boolean

AbstractList.contains


containsAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:83

Returns true if this collection contains every element of c (calls contains for each).

Collection<unknown>

boolean

NullPointerException if c is null

AbstractList.containsAll


ensureCapacity(_minCapacity): void

Defined in: src/list/ArrayList.ts:89

Does nothing: capacity is only a hint, and the native array manages its own storage. Kept for API compatibility; the argument is not validated.

number

void


equals(o): boolean

Defined in: src/list/ArrayList.ts:428

Returns true if o is a List of any class with equal elements in the same order. O(n).

unknown

boolean

AbstractList.equals


forEach(action): void

Defined in: src/list/ArrayList.ts:276

Calls action for each element, in index order.

(e) => void

void

NullPointerException if action is null

ConcurrentModificationException if action structurally modifies this list

AbstractList.forEach


get(index): E

Defined in: src/list/ArrayList.ts:118

Returns the element at index. O(1).

number

E

IndexOutOfBoundsException if index < 0 or index >= size()

AbstractList.get


getFirst(): E

Defined in: src/list/ArrayList.ts:386

Returns the first element. O(1).

E

NoSuchElementException if the list is empty

AbstractList.getFirst


getLast(): E

Defined in: src/list/ArrayList.ts:395

Returns the last element. O(1).

E

NoSuchElementException if the list is empty

AbstractList.getLast


hashCode(): number

Defined in: src/list/ArrayList.ts:454

Returns the list hash code, h = 31 * h + hash(e) from 1 in 32-bit arithmetic, consistent with equals. O(n).

number

AbstractList.hashCode


indexOf(o): number

Defined in: src/list/ArrayList.ts:208

Returns the index of the first element equal to o (by Objects.equals), or -1 if there is none. O(n) scan from the front; NaN matches NaN.

unknown

number

AbstractList.indexOf


isEmpty(): boolean

Defined in: src/list/ArrayList.ts:110

Returns true if this list has no elements. O(1).

boolean

AbstractList.isEmpty


iterator(): Iterator<E>

Defined in: src/list/ArrayList.ts:402

Returns a fail-fast iterator over the elements in index order. Its remove is O(n), like removeAt.

Iterator<E>

AbstractList.iterator


lastIndexOf(o): number

Defined in: src/list/ArrayList.ts:234

Returns the index of the last element equal to o (by Objects.equals), or -1 if there is none. O(n) scan from the back; NaN matches NaN.

unknown

number

AbstractList.lastIndexOf


listIterator(index?): ListIterator<E>

Defined in: src/list/ArrayList.ts:412

Returns a fail-fast list iterator starting at index: the first next() returns the element at index. Its set is O(1); its add and remove are O(n), like the positional methods.

number = 0

start position, 0 by default

ListIterator<E>

IndexOutOfBoundsException if index < 0 or index > size()

AbstractList.listIterator


parallelStream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:180

Returns a stream over spliterator() flagged as parallel.

Stream<E>

AbstractList.parallelStream


remove(o): boolean

Defined in: src/list/ArrayList.ts:192

Removes the first element equal to o (by Objects.equals), if present. O(n): a linear scan, then a shift of the tail.

unknown

boolean

true if an element was removed

AbstractList.remove


removeAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:108

Removes every element that c contains, through the iterator’s remove(); returns true if any was removed.

Collection<unknown>

boolean

NullPointerException if c is null

AbstractList.removeAll


removeAt(index): E

Defined in: src/list/ArrayList.ts:175

Removes the element at index, shifting later elements one position to the left. O(n − index); O(1) for the last element.

number

E

the removed element

IndexOutOfBoundsException if index < 0 or index >= size()

AbstractList.removeAt


removeFirst(): E

Defined in: src/abstract/AbstractList.ts:271

Removes and returns the element at index 0, via removeAt.

E

NoSuchElementException if the list is empty

AbstractList.removeFirst


removeIf(filter): boolean

Defined in: src/list/ArrayList.ts:321

Removes every element for which filter returns true, in O(n) with one compaction pass. Every element is tested before anything is removed, so a throwing predicate, or one that modifies the list, leaves the list unchanged.

(e) => boolean

boolean

true if any element was removed

NullPointerException if filter is null

ConcurrentModificationException if filter structurally modifies this list

AbstractList.removeIf


removeLast(): E

Defined in: src/abstract/AbstractList.ts:280

Removes and returns the last element, via removeAt.

E

NoSuchElementException if the list is empty

AbstractList.removeLast


removeRange(fromIndex, toIndex): void

Defined in: src/list/ArrayList.ts:303

Removes [fromIndex, toIndex). Public so views can delegate.

number

number

void

IndexOutOfBoundsException for a bad range, before anything is removed

AbstractList.removeRange


replaceAll(operator): void

Defined in: src/list/ArrayList.ts:351

Replaces each element with the result of operator applied to it, in order. O(n). Counts as a structural change: open iterators fail fast afterwards.

(e) => E

void

NullPointerException if operator is null

ConcurrentModificationException if operator structurally modifies this list

AbstractList.replaceAll


retainAll(c): boolean

Defined in: src/abstract/AbstractCollection.ts:118

Removes every element that c does not contain, through the iterator’s remove(); returns true if any was removed.

Collection<unknown>

boolean

NullPointerException if c is null

AbstractList.retainAll


reversed(): List<E>

Defined in: src/abstract/AbstractList.ts:286

Returns a live reverse-ordered view; writes through it reach this list. reversed().reversed() is this list.

List<E>

AbstractList.reversed


set(index, e): E

Defined in: src/list/ArrayList.ts:130

Replaces the element at index with e. O(1); not a structural change, so open iterators stay valid.

number

E

E

the element previously at index

IndexOutOfBoundsException if index < 0 or index >= size()

AbstractList.set


size(): number

Defined in: src/list/ArrayList.ts:105

Returns the number of elements. O(1).

number

AbstractList.size


sort(c?): void

Defined in: src/list/ArrayList.ts:369

Sorts this list in place. The sort is stable (equal elements keep their order) and O(n log n). Counts as a structural change: open iterators fail fast afterwards.

Comparator<E> | null = null

the comparator; null or omitted means natural ordering

void

ClassCastException if natural ordering is used and the elements have none

ConcurrentModificationException if the comparator structurally modifies this list

AbstractList.sort


spliterator(): Spliterator<E>

Defined in: src/list/ArrayList.ts:418

Returns a spliterator over the elements, used to build streams: ORDERED | SIZED | SUBSIZED, late-binding, fail-fast, splits in halves.

Spliterator<E>

AbstractList.spliterator


stream(): Stream<E>

Defined in: src/abstract/AbstractCollection.ts:175

Returns a sequential stream over spliterator().

Stream<E>

AbstractList.stream


subList(fromIndex, toIndex): List<E>

Defined in: src/abstract/AbstractList.ts:193

Returns a live view of [fromIndex, toIndex): changes through it write to this list, and a structural change made to this list directly makes the view throw ConcurrentModificationException. Carries the RandomAccess brand when this list does.

number

number

List<E>

IndexOutOfBoundsException if fromIndex < 0 or toIndex > size()

IllegalArgumentException if fromIndex > toIndex

AbstractList.subList


toArray(): E[]

Defined in: src/list/ArrayList.ts:255

Returns a new array with all elements, in index order. Changing the array does not affect this list. O(n).

E[]

AbstractList.toArray

toArray<A>(generator): A

Defined in: src/list/ArrayList.ts:260

Fills an array created by generator(size()) with the elements in index order, and returns it. O(n).

A extends WritableArrayLike<E>

(length) => A

creates the target array for a given length

A

AbstractList.toArray


toString(): string

Defined in: src/list/ArrayList.ts:465

Returns the elements in index order as [a, b, c]; an element that is this list prints as (this Collection).

string

AbstractList.toString


trimToSize(): void

Defined in: src/list/ArrayList.ts:95

Does not change storage (the native array manages it), but counts as a structural change, so open iterators fail fast afterwards.

void


static of<E>(…xs): ArrayList<E>

Defined in: src/list/ArrayList.ts:68

Creates a mutable list holding xs in argument order. Unlike List.of, the result is modifiable and may hold null and undefined. Called on a subclass, returns an instance of it.

E

…E[]

ArrayList<E>