javascript-collections-framework
    Preparing search index...

    Class ArrayList<E>Sealed

    A list stored in an array: indexed reads and writes in O(1), appends in amortised O(1), inserts and removals in time proportional to the elements after the position. Elements may be anything, null and undefined included.

    Unlike an array, an index out of range throws IndexOutOfBoundsError instead of reading undefined; delete(element) removes by value; subList and reversed are live views; and adding, removing or sorting while iterating throws ConcurrentModificationError (use a cursor to change the list while iterating). indexOf, has and delete compare with SameValueZero, so NaN is found; equals compares two lists element by element with equals(a, b).

    The class is not designed to be subclassed.

    const list = new ArrayList([3, 1, 2])
    list.add(4).insert(0, 0) // [0, 3, 1, 2, 4]
    list.sort() // [0, 1, 2, 3, 4]
    list.subList(1, 3).clear() // [0, 3, 4]
    list.get(5) // throws IndexOutOfBoundsError

    Type Parameters

    • E

    Implements

    Index
    • Creates a list holding the elements of elements, in order; empty when elements is omitted, undefined or null (as new Set(null) is).

      Time: O(m) for m elements.

      Type Parameters

      • E

      Parameters

      • Optionalelements: Iterable<E, any, any> | null

        The initial elements.

      Returns ArrayList<E>

      TypeError when elements is neither iterable nor null/undefined.

      new ArrayList<string>()       // ArrayList(0) []
      new ArrayList('abc') // ArrayList(3) ['a', 'b', 'c']
    • get "[ARRAY_LIST_BRAND]"(): true

      Marks instances of this class so instanceof recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[COLLECTION_BRAND]"(): true

      Marks collections so isCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[LIST_BRAND]"(): true

      Marks lists so isList recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[SEQUENCED_COLLECTION_BRAND]"(): true

      Marks sequenced collections so isSequencedCollection recognises them across copies of the package. Its key is not exported, so the getter is not called directly. Time: O(1).

      Returns true

    • get "[toStringTag]"(): string

      'ArrayList', for Object.prototype.toString. Time: O(1).

      Returns string

    • get revision(): number

      A number that changes on every change to the contents — adding, removing, replacing or sorting elements — and never on a read. Its exact values are unspecified. Time: O(1).

      Returns number

    • get size(): number

      The number of elements. Time: O(1).

      Returns number

    • The value-equality protocol; delegates to equals. Time: as equals.

      Parameters

      • other: unknown

      Returns boolean

    • The value-equality protocol: equal lists give equal hashes. The value is unspecified. Time: O(n) plus element hashes.

      Parameters

      Returns number

    • Node's inspect hook: ArrayList(2) [ 1, 2 ] in console.log. Time: as toString.

      Parameters

      • depth: number | null
      • options: object
      • inspect: (value: unknown, options: object) => string

      Returns string

    • Returns an iterator over the elements in index order.

      Time: O(1) to create; O(1) per step.

      Returns IterableIterator<E>

      A new iterator.

      ConcurrentModificationError (from next()) when the list is changed structurally (elements added or removed, or a sort) after the iterator is created, including through a cursor.

      for (const element of new ArrayList([1, 2])) console.log(element)
      
    • Appends element and returns the list, so calls chain.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to append.

      Returns this

      The list.

      new ArrayList<number>().add(1).add(2) // ArrayList(2) [1, 2]
      
    • Appends every element of elements, in order, after reading them all.

      Time: O(m) amortised for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to append.

      Returns boolean

      true when any element was added.

      TypeError when elements is not iterable.

      new ArrayList([1]).addAll([2, 3]) // true; [1, 2, 3]
      
    • Inserts element at the front and returns the list.

      Time: O(n).

      Parameters

      • element: E

        The element to insert.

      Returns this

      The list.

      new ArrayList([2]).addFirst(1) // ArrayList(2) [1, 2]
      
    • Appends element and returns the list; the same as add.

      Time: amortised O(1).

      Parameters

      • element: E

        The element to append.

      Returns this

      The list.

      new ArrayList([1]).addLast(2) // ArrayList(2) [1, 2]
      
    • Removes every element.

      Time: O(n).

      Returns void

      list.clear() // list.size === 0
      
    • Returns a new ArrayList with the same elements, independent of this one (the elements themselves are not copied).

      Time: O(n).

      Returns ArrayList<E>

      The copy.

      const copy = list.copy()
      copy.add(1) // list is unchanged
    • Returns a cursor positioned before the first element; the same as listCursor().

      Time: O(1).

      Returns ListCursor<E>

      A new cursor.

      const cursor = list.cursor()
      while (cursor.hasNext()) if (cursor.next() < 0) cursor.remove()
    • Removes the first element equal to element under SameValueZero. To remove by position, use removeAt: on a list of numbers, delete(1) removes the value 1, not index 1.

      Time: O(n).

      Parameters

      • element: E

        The value to remove.

      Returns boolean

      true when an element was removed.

      new ArrayList([3, 1, 3]).delete(3) // true; [1, 3]
      
    • Returns whether other is a list (any implementation or view, from any copy of the package) with the same size whose elements are equal in order under equals(a, b).

      Time: O(n) plus element comparisons.

      Parameters

      • other: unknown

        Any value.

      Returns boolean

      true when equal.

      new ArrayList([1, 2]).equals(new ArrayList([1, 2])) // true
      new ArrayList([1, 2]).equals([1, 2]) // false: an array is not a list
    • Calls action(element, index, list) for each element in index order, with this set to thisArg, as Array.prototype.forEach does.

      Time: O(n) plus the calls.

      Parameters

      • action: (element: E, index: number, list: List<E>) => void

        Called with each element, its index and the list.

      • OptionalthisArg: unknown

        The this value for action.

      Returns void

      TypeError when action is not a function.

      ConcurrentModificationError when action adds or removes elements.

      new ArrayList(['a', 'b']).forEach((s, i) => console.log(i, s))
      
    • Returns the element at index.

      Time: O(1).

      Parameters

      • index: number

        An integer with 0 <= index < size.

      Returns E

      The element.

      IndexOutOfBoundsError when index is not an integer in range.

      new ArrayList(['a', 'b']).get(1) // 'b'
      
    • Returns the first element.

      Time: O(1).

      Returns E

      The element at index 0.

      NoSuchElementError when the list is empty.

      new ArrayList([1, 2]).getFirst() // 1
      
    • Returns the last element.

      Time: O(1).

      Returns E

      The element at index size - 1.

      NoSuchElementError when the list is empty.

      new ArrayList([1, 2]).getLast() // 2
      
    • Returns whether some element is equal to element under SameValueZero.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns boolean

      true when present.

      new ArrayList([null]).has(null) // true
      
    • Returns the lowest index whose element is equal to element under SameValueZero, or -1. Unlike Array.prototype.indexOf, NaN is found.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns number

      The index, or -1.

      new ArrayList([1, Number.NaN, 1]).indexOf(Number.NaN) // 1
      
    • Inserts element at index, shifting later elements up by one, and returns the list.

      Time: O(size − index).

      Parameters

      • index: number

        An integer with 0 <= index <= size.

      • element: E

        The element to insert.

      Returns this

      The list.

      IndexOutOfBoundsError when index is not an integer in range.

      new ArrayList(['a', 'c']).insert(1, 'b') // ArrayList(3) ['a', 'b', 'c']
      
    • Inserts every element of elements at index, in order, after reading them all; a list may insert itself.

      Time: O(size + m) for m elements.

      Parameters

      • index: number

        An integer with 0 <= index <= size.

      • elements: Iterable<E>

        The elements to insert.

      Returns boolean

      true when any element was inserted.

      IndexOutOfBoundsError when index is not an integer in range (checked first).

      TypeError when elements is not iterable.

      ConcurrentModificationError when reading elements changed the list.

      new ArrayList([1, 4]).insertAll(1, [2, 3]) // true; [1, 2, 3, 4]
      
    • Returns whether the list has no elements.

      Time: O(1).

      Returns boolean

      size === 0.

      new ArrayList().isEmpty() // true
      
    • Returns the highest index whose element is equal to element under SameValueZero, or -1.

      Time: O(n).

      Parameters

      • element: E

        The value to look for.

      Returns number

      The index, or -1.

      new ArrayList([1, 2, 1]).lastIndexOf(1) // 2
      
    • Returns a cursor positioned before index (0 when omitted) that moves both ways and can replace, insert and remove at its position.

      Time: O(1).

      Parameters

      • Optionalindex: number

        An integer with 0 <= index <= size.

      Returns ListCursor<E>

      A new cursor.

      IndexOutOfBoundsError when index is not an integer in range.

      const cursor = list.listCursor(list.size)
      while (cursor.hasPrevious()) console.log(cursor.previous())
    • Removes every element equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to remove.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new ArrayList([1, 2, 1, 3]).removeAll([1]) // true; [2, 3]
      
    • Removes and returns the element at index, shifting later elements down by one.

      Time: O(size − index).

      Parameters

      • index: number

        An integer with 0 <= index < size.

      Returns E

      The removed element.

      IndexOutOfBoundsError when index is not an integer in range.

      new ArrayList([10, 20, 30]).removeAt(1) // 20; [10, 30]
      
    • Removes and returns the first element.

      Time: O(n).

      Returns E

      The removed element.

      NoSuchElementError when the list is empty.

      new ArrayList([1, 2]).removeFirst() // 1
      
    • Calls predicate(element, index, list) on every element (indexes before any removal), then removes those for which it returned a truthy value, as Array.prototype.filter reads its callback. An error from predicate removes nothing.

      Time: O(n) plus the calls.

      Parameters

      • predicate: (element: E, index: number, list: List<E>) => unknown

        Called with each element, its index and the list.

      Returns boolean

      true when any element was removed.

      TypeError when predicate is not a function.

      ConcurrentModificationError when predicate adds or removes elements.

      new ArrayList([1, -2, 3]).removeIf((n) => n < 0) // true; [1, 3]
      
    • Removes and returns the last element.

      Time: O(1).

      Returns E

      The removed element.

      NoSuchElementError when the list is empty.

      new ArrayList([1, 2]).removeLast() // 2
      
    • Replaces each element with op(element, index, list), in index order. If op throws, the elements before it are already replaced and the rest are unchanged. Open iterators stay valid.

      Time: O(n) plus the calls.

      Parameters

      • op: (element: E, index: number, list: List<E>) => E

        Returns the replacement for each element.

      Returns void

      TypeError when op is not a function.

      ConcurrentModificationError when op adds or removes elements; the result of that call is not stored.

      new ArrayList([1, 2]).replaceAll((n) => n * 10) // [10, 20]
      
    • Removes every element not equal to one in elements (SameValueZero). Decides first, so an error while deciding removes nothing.

      Time: O(n + m) for m elements.

      Parameters

      • elements: Iterable<E>

        The elements to keep.

      Returns boolean

      true when any element was removed.

      TypeError when elements is not iterable.

      new ArrayList([1, 2, 3]).retainAll([2]) // true; [2]
      
    • Returns a live view of this list in reverse order: index i of the view is index size - 1 - i of this list. reversed().reversed() returns this list.

      Time: O(1); each operation on the view costs what it costs on this list at the mirrored index.

      Returns List<E>

      The reversed view.

      const list = new ArrayList([1, 2, 3])
      list.reversed().getFirst() // 3
      list.reversed().addFirst(4) // list is [1, 2, 3, 4]
    • Replaces the element at index and returns the old one. revision changes; open iterators stay valid.

      Time: O(1).

      Parameters

      • index: number

        An integer with 0 <= index < size.

      • element: E

        The new element.

      Returns E

      The element that was at index.

      IndexOutOfBoundsError when index is not an integer in range.

      new ArrayList(['a', 'b']).set(0, 'z') // 'a'; ['z', 'b']
      
    • Sorts the list in place, keeping equal elements in their order (stable), and returns it. Without a comparator, natural order is used (numbers by value, strings by code unit, …). Unlike Array.prototype.sort, null and undefined reach the comparator like any other element (natural order rejects them: sort with nullsLast(naturalOrder()) to put them last), and a comparator result that is not a number throws. If anything throws, the sort itself writes nothing to the list.

      Time: O(n log n); at most n·⌈log₂ n⌉ comparator calls.

      Parameters

      • Optionalcomparator: Comparator<E>

        The order; natural order when omitted.

      Returns this

      The list.

      TypeError when comparator is neither a function nor undefined, or returns NaN or a non-number.

      ClassCastError when natural order cannot compare two elements.

      NullElementError when natural order meets null or undefined.

      ConcurrentModificationError when the comparator adds or removes elements; its changes stay and the sort makes none of its own.

      new ArrayList(['b', 'a']).sort()                     // ['a', 'b']
      new ArrayList([1, 2, 3]).sort(reverseOrder()) // [3, 2, 1]
      new ArrayList([2, null, 1]).sort(nullsLast(naturalOrder<number>())) // [1, 2, null]
    • Returns a live view of the elements from from (inclusive) to to (exclusive). Changes through the view reach this list. Once this list is changed structurally (elements added or removed, or a sort) other than through the view, the view throws ConcurrentModificationError.

      Time: O(1); each operation on the view costs what it costs on this list at the mapped index; a change of size also updates each enclosing sub-list, one step per level of nesting.

      Parameters

      • from: number

        Start of the range, inclusive.

      • to: number

        End of the range, exclusive.

      Returns List<E>

      The view.

      IndexOutOfBoundsError when from < 0, to > size or a bound is not an integer.

      IllegalArgumentError when from > to.

      const list = new ArrayList([0, 1, 2, 3, 4])
      list.subList(1, 4).clear() // list is [0, 4]
    • Returns a new array of the elements, in order.

      Time: O(n).

      Returns E[]

      A new array.

      new ArrayList([1, 2]).toArray() // [1, 2]
      
    • Returns a new array of the elements, so JSON.stringify writes the list as an array.

      Time: O(n).

      Returns E[]

      A new array.

      JSON.stringify(new ArrayList([1, 2])) // '[1,2]'
      
    • Returns the class name, size and elements, e.g. ArrayList(3) [1, 2, 'a'].

      Time: O(length of the output) for flat contents.

      Returns string

      The text.

      String(new ArrayList([1, 'a'])) // "ArrayList(2) [1, 'a']"
      
    • Returns whether value is an ArrayList, including one created by another copy of the library with the same major version. Views (subList, reversed) are not ArrayLists.

      Time: O(1).

      Parameters

      • value: unknown

        The value on the left of instanceof.

      Returns boolean

      true when value is an ArrayList.

      new ArrayList() instanceof ArrayList // true
      
    • Returns a new list of the elements of elements, in order, like Array.from but for iterables only.

      Time: O(m) for m elements.

      Type Parameters

      • T

      Parameters

      • elements: Iterable<T>

        The elements.

      Returns ArrayList<T>

      A new list.

      TypeError when elements is not iterable (null, undefined and array-likes such as { length: 2 } included).

      ArrayList.from(new Set([1, 2])) // ArrayList(2) [1, 2]
      
    • Returns a new list of mapFn(element, index) for each element of elements, in order, with this set to thisArg, as Array.from does. elements is read while mapFn runs, so items mapFn appends to an array source are included.

      Time: O(m) plus the calls.

      Type Parameters

      • T
      • U

      Parameters

      • elements: Iterable<T>

        The elements.

      • mapFn: (element: T, index: number) => U

        Called with each element and its index; its result is stored.

      • OptionalthisArg: unknown

        The this value for mapFn.

      Returns ArrayList<U>

      A new list.

      TypeError when mapFn is not a function (checked before elements is read), or elements is not iterable.

      Error whatever mapFn throws; no collection is returned.

      ArrayList.from([1, 2, 3], (n) => n * 2) // ArrayList(3) [2, 4, 6]