Skip to content
Buy me a coffee

Stack

Defined in: src/list/Stack.ts:12

Legacy last-in-first-out stack built on Vector. The top of the stack is the end of the vector, so push, pop and peek are O(1). An empty pop() or peek() throws EmptyStackException. Permits null and undefined.

Iteration, toString() and toArray() go bottom to top (index order), and every list method stays available. For new code, use ArrayDeque as a stack.

E

new Stack<E>(): Stack<E>

Defined in: src/list/Vector.ts:34

Creates an empty vector with capacity 10 that doubles when outgrown.

Stack<E>

Vector.constructor

new Stack<E>(initialCapacity, capacityIncrement?): Stack<E>

Defined in: src/list/Vector.ts:41

Creates an empty vector with the given reported capacity.

number

initial value of capacity()

number

amount the capacity grows by when outgrown; 0 or less (the default) means it doubles

Stack<E>

IllegalArgumentException if initialCapacity is negative or not an integer

Vector.constructor

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

Defined in: src/list/Vector.ts:46

Creates a vector holding the elements of c, in its iteration order; capacity() is their count.

Collection<E>

Stack<E>

NullPointerException if c is null or undefined

Vector.constructor

readonly capacityIncrement: number

Defined in: src/list/Vector.ts:31

Amount capacity() grows by when outgrown; 0 or less means it doubles. Fixed at construction.

Vector.capacityIncrement


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.

Vector.modCount

get [RANDOM_ACCESS](): true

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

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

true

Vector.[RANDOM_ACCESS]

[INSPECT](): string

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

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

string

Vector.[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>

Vector.[iterator]


add(e): boolean

Defined in: src/list/Vector.ts:98

Appends e to the end of this vector, in amortized O(1), growing capacity() when outgrown.

E

boolean

always true

Vector.add

add(index, e): void

Defined in: src/list/Vector.ts:104

Inserts e at index, shifting later elements one position to the right. O(n − index); grows capacity() when outgrown.

number

E

void

ArrayIndexOutOfBoundsException if index < 0 or index > size()

Vector.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

Vector.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

Vector.addAll


addElement(e): void

Defined in: src/list/Vector.ts:298

Appends e; same as add(e) but returns nothing.

E

void

Vector.addElement


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

Vector.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

Vector.addLast


capacity(): number

Defined in: src/list/Vector.ts:183

Returns the reported capacity: how many elements fit before it next grows. It is bookkeeping for ported code; the engine manages the real storage.

number

Vector.capacity


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

Vector.clear


clone(): this

Defined in: src/list/Vector.ts:144

Returns a shallow, independent copy whose capacity() equals its size; capacityIncrement is kept.

this

Vector.clone


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

Vector.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

Vector.containsAll


copyInto(anArray): void

Defined in: src/list/Vector.ts:233

Copies the elements, in index order, into anArray from index 0. A plain array grows if too short; a fixed-size (typed) array that is too short throws IndexOutOfBoundsException.

WritableArrayLike<E>

void

NullPointerException if anArray is null

Vector.copyInto


elementAt(index): E

Defined in: src/list/Vector.ts:274

Returns the element at index; same as get. O(1).

number

E

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

Vector.elementAt


elements(): Enumeration<E> & Iterable<E, any, any>

Defined in: src/list/Vector.ts:241

Returns a legacy Enumeration of the elements in index order. It is index-based and not fail-fast; it is also JS-iterable and adapts to an Iterator through asIterator().

Enumeration<E> & Iterable<E, any, any>

Vector.elements


empty(): boolean

Defined in: src/list/Stack.ts:49

Returns true if the stack has no elements; same as isEmpty.

boolean


ensureCapacity(minCapacity): void

Defined in: src/list/Vector.ts:162

Raises capacity() to at least minCapacity: it grows by capacityIncrement (or doubles when that is 0 or less), or straight to minCapacity if that is larger. Real storage is unaffected. Any positive request counts as a structural change, so open iterators fail fast afterwards.

number

void

IllegalArgumentException if minCapacity is not an integer

Vector.ensureCapacity


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

Vector.equals


firstElement(): E

Defined in: src/list/Vector.ts:283

Returns the first element; same as getFirst.

E

NoSuchElementException if the vector is empty

Vector.firstElement


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

Vector.forEach


get(index): E

Defined in: src/list/Vector.ts:61

Returns the element at index. O(1).

number

E

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

Vector.get


getFirst(): E

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

Returns the first element. O(1).

E

NoSuchElementException if the list is empty

Vector.getFirst


getLast(): E

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

Returns the last element. O(1).

E

NoSuchElementException if the list is empty

Vector.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

Vector.hashCode


indexOf(o, index?): number

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

Returns the index of the first element equal to o, searching forward from index (from 0 when omitted), or -1. O(n).

unknown

number

number

IndexOutOfBoundsException if index < 0

Vector.indexOf


insertElementAt(e, index): void

Defined in: src/list/Vector.ts:330

Inserts e at index; same as add(index, e) with the arguments swapped. O(n − index).

E

number

void

ArrayIndexOutOfBoundsException if index < 0 or index > size()

Vector.insertElementAt


isEmpty(): boolean

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

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

boolean

Vector.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>

Vector.iterator


lastElement(): E

Defined in: src/list/Vector.ts:292

Returns the last element; same as getLast.

E

NoSuchElementException if the vector is empty

Vector.lastElement


lastIndexOf(o, index?): number

Defined in: src/list/Vector.ts:206

Returns the index of the last element equal to o, searching backward from index (from the end when omitted), or -1. O(n).

unknown

number

number

IndexOutOfBoundsException if index >= size()

Vector.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()

Vector.listIterator


parallelStream(): Stream<E>

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

Returns a stream over spliterator() flagged as parallel.

Stream<E>

Vector.parallelStream


peek(): E

Defined in: src/list/Stack.ts:42

Returns the top element without removing it. O(1).

E

EmptyStackException if the stack is empty


pop(): E

Defined in: src/list/Stack.ts:31

Removes and returns the top element. O(1).

E

EmptyStackException if the stack is empty


push(e): E

Defined in: src/list/Stack.ts:22

Pushes e onto the top in amortized O(1); same as addElement.

E

E

e


remove(o): boolean

Defined in: src/list/Vector.ts:307

Removes the first element equal to o (by Objects.equals), if present. O(n). Counts as a structural change even when o is not found, so open iterators fail fast afterwards.

unknown

boolean

true if an element was removed

Vector.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

Vector.removeAll


removeAllElements(): void

Defined in: src/list/Vector.ts:346

Removes every element; same as clear. capacity() is unchanged.

void

Vector.removeAllElements


removeAt(index): E

Defined in: src/list/Vector.ts:82

Removes the element at index, shifting later elements one position to the left. O(n − index). Counts as a structural change even when index is invalid.

number

E

the removed element

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

Vector.removeAt


removeElement(o): boolean

Defined in: src/list/Vector.ts:313

Removes the first element equal to o; same as remove(o). O(n).

unknown

boolean

Vector.removeElement


removeElementAt(index): void

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

Removes the element at index; same as removeAt but returns nothing. O(n − index).

number

void

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

Vector.removeElementAt


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

Vector.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

Vector.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

Vector.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

Vector.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

Vector.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

Vector.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>

Vector.reversed


search(o): number

Defined in: src/list/Stack.ts:57

Returns the 1-based distance of o from the top (the top itself is 1), using the occurrence nearest the top, or -1 when absent. O(n).

unknown

number


set(index, e): E

Defined in: src/list/Vector.ts:71

Replaces the element at index with e. O(1); not a structural change.

number

E

E

the element previously at index

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

Vector.set


setElementAt(e, index): void

Defined in: src/list/Vector.ts:340

Replaces the element at index; same as set(index, e) with the arguments swapped, but returns nothing. O(1), not a structural change.

E

number

void

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

Vector.setElementAt


setSize(newSize): void

Defined in: src/list/Vector.ts:219

Sets the size: truncates the vector, or pads it with null elements. Raises capacity() if needed.

number

void

ArrayIndexOutOfBoundsException if newSize < 0 or not an integer

Vector.setSize


size(): number

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

Returns the number of elements. O(1).

number

Vector.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

Vector.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>

Vector.spliterator


stream(): Stream<E>

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

Returns a sequential stream over spliterator().

Stream<E>

Vector.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

Vector.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[]

Vector.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

Vector.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

Vector.toString


trimToSize(): void

Defined in: src/list/Vector.ts:174

Sets capacity() to size(). Counts as a structural change, so open iterators fail fast afterwards.

void

Vector.trimToSize


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

Defined in: src/list/Stack.ts:14

Creates a stack holding xs pushed in argument order, so the last argument is on top.

E

…E[]

Stack<E>

Vector.of