Dart API Referencedart:coreList<E>

List<E> interface

A List is an indexable collection with a length. It can be of fixed size or extendable.

interface List<E> extends Collection<E>, Sequence<E>
                  default ListImplementation<E> {
  /**
   * Creates a list of the given [length].
   *
   * If no [length] argument is supplied an extendable list of
   * length 0 is created.
   *
   * If a [length] argument is supplied, a fixed size list of that
   * length is created.
   */
  List([int length]);

  /**
   * Creates a list with the elements of [other]. The order in
   * the list will be the order provided by the iterator of [other].
   */
  List.from(Iterable<E> other);

  /**
   * Returns the element at the given [index] in the list or throws
   * an [IndexOutOfRangeException] if [index] is out of bounds.
   */
  E operator [](int index);

  /**
   * Sets the entry at the given [index] in the list to [value].
   * Throws an [IndexOutOfRangeException] if [index] is out of bounds.
   */
  void operator []=(int index, E value);

  /**
   * Changes the length of the list. If [newLength] is greater than
   * the current [length], entries are initialized to [:null:]. Throws
   * an [UnsupportedOperationException] if the list is not extendable.
   */
  void set length(int newLength);

  /**
   * Adds [value] at the end of the list, extending the length by
   * one. Throws an [UnsupportedOperationException] if the list is not
   * extendable.
   */
  void add(E value);

  /**
   * Adds [value] at the end of the list, extending the length by
   * one. Throws an [UnsupportedOperationException] if the list is not
   * extendable.
   */
  void addLast(E value);

  /**
   * Appends all elements of the [collection] to the end of this list.
   * Extends the length of the list by the number of elements in [collection].
   * Throws an [UnsupportedOperationException] if this list is not
   * extendable.
   */
  void addAll(Collection<E> collection);

  /**
   * Sorts the list according to the order specified by the [Comparator].
   */
  void sort([Comparator<E> compare = Comparable.compare]);

  /**
   * Returns the first index of [element] in the list.
   *
   * Searches the list from index [start] to the length of the list.
   * The first time an element [:e:] is encountered so that [:e == element:],
   * the index of [:e:] is returned.
   * Returns -1 if [element] is not found.
   */
  int indexOf(E element, [int start = 0]);

  /**
   * Returns the last index of [element] in the list.
   *
   * Searches the list backwards from index [start] (inclusive) to 0.
   * The first time an element [:e:] is encountered so that [:e == element:],
   * the index of [:e:] is returned.
   * If start is not provided, it defaults to [:this.length - 1:] .
   * Returns -1 if [element] is not found.
   */
  int lastIndexOf(E element, [int start]);

  /**
   * Removes all elements in the list.
   *
   * The length of the list becomes zero.
   * Throws an [UnsupportedOperationException], and retains all elements, if the
   * length of the list cannot be changed.
   */
  void clear();

  /**
   * Removes the element at position[index] from the list.
   *
   * This reduces the length of the list by one and moves all later elements
   * down by one position.
   * Returns the removed element.
   * Throws an [ArgumentError] if [index] is not an [int].
   * Throws an [IndexOutOfRangeException] if the [index] does not point inside
   * the list.
   * Throws an [UnsupportedOperationException], and doesn't remove the element,
   * if the length of the list cannot be changed.
   */
  E removeAt(int index);

  /**
   * Pops and returns the last element of the list.
   * Throws a [UnsupportedOperationException] if the length of the
   * list cannot be changed.
   */
  E removeLast();

  /**
   * Returns the last element of the list, or throws an out of bounds
   * exception if the list is empty.
   */
  E last();

  /**
   * Returns a new list containing [length] elements from the list,
   * starting at  [start].
   * Returns an empty list if [length] is 0.
   * Throws an [ArgumentError] if [length] is negative.
   * Throws an [IndexOutOfRangeException] if [start] or
   * [:start + length - 1:] are out of range.
   */
  List<E> getRange(int start, int length);

  /**
   * Copies [length] elements of [from], starting
   * at [startFrom], into the list, starting at [start].
   * If [length] is 0, this method does not do anything.
   * Throws an [ArgumentError] if [length] is negative.
   * Throws an [IndexOutOfRangeException] if [start] or
   * [:start + length - 1:] are out of range for [:this:], or if
   * [startFrom] or [:startFrom + length - 1:] are out of range for [from].
   */
  void setRange(int start, int length, List<E> from, [int startFrom]);

  /**
   * Removes [length] elements from the list, beginning at [start].
   * Throws an [UnsupportedOperationException] if the list is
   * not extendable.
   * If [length] is 0, this method does not do anything.
   * Throws an [ArgumentError] if [length] is negative.
   * Throws an [IndexOutOfRangeException] if [start] or
   * [:start + length: - 1] are out of range.
   */
  void removeRange(int start, int length);

  /**
   * Inserts a new range into the list, starting from [start] to
   * [:start + length - 1:]. The entries are filled with [initialValue].
   * Throws an [UnsupportedOperationException] if the list is
   * not extendable.
   * If [length] is 0, this method does not do anything.
   * If [start] is the length of the list, this method inserts the
   * range at the end of the list.
   * Throws an [ArgumentError] if [length] is negative.
   * Throws an [IndexOutOfRangeException] if [start] is negative or if
   * [start] is greater than the length of the list.
   */
  void insertRange(int start, int length, [E initialValue]);
}

Default class

ListImplementation<E>

Extends

Sequence<T>, Collection<E>

Implemented by

CSSTransformValue, DOMMimeTypeArray, DOMPluginArray, Float32Array, Float32List, Float64Array, Float64List, HTMLAllCollection, HTMLCollection, Int16Array, Int16List, Int32Array, Int32List, Int64List, Int8Array, Int8List, NamedNodeMap, NodeList, SQLResultSetRowList, SVGAnimatedLengthList, SVGAnimatedNumberList, SVGAnimatedTransformList, SVGLengthList, SVGNumberList, SVGPathSegList, SVGStringList, SVGTransformList, SequenceList<E>, SourceBufferList, SpeechGrammarList, TextTrackCueList, TextTrackList, TouchList, Uint16Array, Uint16List, Uint32Array, Uint32List, Uint64List, Uint8Array, Uint8List, WebKitCSSFilterValue

Constructors

new List([int length]) #

Creates a list of the given length.

If no length argument is supplied an extendable list of length 0 is created.

If a length argument is supplied, a fixed size list of that length is created.

List([int length]);

new List.from(Iterable<E> other) #

Creates a list with the elements of other. The order in the list will be the order provided by the iterator of other.

List.from(Iterable<E> other);

Properties

int get length #

inherited from Sequence

The limit of valid indices of the sequence.

The length getter should be efficient.

int get length;

void set length(int newLength) #

Changes the length of the list. If newLength is greater than the current length, entries are initialized to null. Throws an UnsupportedOperationException if the list is not extendable.

void set length(int newLength);

Operators

E operator [](int index) #

Returns the element at the given index in the list or throws an IndexOutOfRangeException if index is out of bounds.

E operator [](int index);

void operator []=(int index, E value) #

Sets the entry at the given index in the list to value. Throws an IndexOutOfRangeException if index is out of bounds.

void operator []=(int index, E value);

Methods

void add(E value) #

Adds value at the end of the list, extending the length by one. Throws an UnsupportedOperationException if the list is not extendable.

void add(E value);

void addAll(Collection<E> collection) #

Appends all elements of the collection to the end of this list. Extends the length of the list by the number of elements in collection. Throws an UnsupportedOperationException if this list is not extendable.

void addAll(Collection<E> collection);

void addLast(E value) #

Adds value at the end of the list, extending the length by one. Throws an UnsupportedOperationException if the list is not extendable.

void addLast(E value);

void clear() #

Removes all elements in the list.

The length of the list becomes zero. Throws an UnsupportedOperationException, and retains all elements, if the length of the list cannot be changed.

void clear();

bool contains(E element) #

inherited from Collection

Check whether the collection contains an element equal to element.

bool contains(E element) {
  for (E e in this) {
    if (e == element) return true;
  }
  return false;
}

bool every(bool f(E element)) #

inherited from Collection

Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.

bool every(bool f(E element)) {
  for (E element in this) {
    if (!f(element)) return false;
  }
  return true;
}

Collection<E> filter(bool f(E element)) #

inherited from Collection

Returns a collection with the elements of this collection that satisfy the predicate f.

The returned collection should be of the same type as the collection creating it.

An element satisfies the predicate f if f(element) returns true.

Collection<E> filter(bool f(E element));

void forEach(void f(E element)) #

inherited from Collection

Applies the function f to each element of this collection.

void forEach(void f(E element)) {
  for (E element in this) f(element);
}

List<E> getRange(int start, int length) #

Returns a new list containing length elements from the list, starting at start. Returns an empty list if length is 0. Throws an ArgumentError if length is negative. Throws an IndexOutOfRangeException if start or start + length - 1 are out of range.

List<E> getRange(int start, int length);

int indexOf(E element, [int start = 0]) #

Returns the first index of element in the list.

Searches the list from index start to the length of the list. The first time an element e is encountered so that e == element, the index of e is returned. Returns -1 if element is not found.

int indexOf(E element, [int start = 0]);

void insertRange(int start, int length, [E initialValue]) #

Inserts a new range into the list, starting from start to start + length - 1. The entries are filled with initialValue. Throws an UnsupportedOperationException if the list is not extendable. If length is 0, this method does not do anything. If start is the length of the list, this method inserts the range at the end of the list. Throws an ArgumentError if length is negative. Throws an IndexOutOfRangeException if start is negative or if start is greater than the length of the list.

void insertRange(int start, int length, [E initialValue]);

bool isEmpty() #

inherited from Collection

Returns true if there is no element in this collection.

bool isEmpty() => !iterator().hasNext();

Iterator<E> iterator() #

inherited from Iterable

Returns an Iterator that iterates over this Iterable object.

Iterator<E> iterator();

E last() #

Returns the last element of the list, or throws an out of bounds exception if the list is empty.

E last();

int lastIndexOf(E element, [int start]) #

Returns the last index of element in the list.

Searches the list backwards from index start to 0. The first time an element e is encountered so that e == element, the index of e is returned. If start is not provided, it defaults to this.length - 1 . Returns -1 if element is not found.

int lastIndexOf(E element, [int start]);

Collection map(f(E element)) #

inherited from Collection

Returns a new collection with the elements f(e) for each element e of this collection.

Subclasses of Collection should implement the map method to return a collection of the same general type as themselves. E.g., List.map should return a List.

Collection map(f(E element));

reduce(initialValue, combine(previousValue, E element)) #

inherited from Collection

Reduce a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function. Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of a collection:

collection.reduce(0, (prev, element) => prev + element);

Dynamic reduce(var initialValue,
               Dynamic combine(var previousValue, E element)) {
  var value = initialValue;
  for (E element in this) value = combine(value, element);
  return value;
}

E removeAt(int index) #

Removes the element at position index from the list.

This reduces the length of the list by one and moves all later elements down by one position. Returns the removed element. Throws an ArgumentError if index is not an int. Throws an IndexOutOfRangeException if the index does not point inside the list. Throws an UnsupportedOperationException, and doesn't remove the element, if the length of the list cannot be changed.

E removeAt(int index);

E removeLast() #

Pops and returns the last element of the list. Throws a UnsupportedOperationException if the length of the list cannot be changed.

E removeLast();

void removeRange(int start, int length) #

Removes length elements from the list, beginning at start. Throws an UnsupportedOperationException if the list is not extendable. If length is 0, this method does not do anything. Throws an ArgumentError if length is negative. Throws an IndexOutOfRangeException if start or :start + length: - 1 are out of range.

void removeRange(int start, int length);

void setRange(int start, int length, List<E> from, [int startFrom]) #

Copies length elements of from, starting at startFrom, into the list, starting at start. If length is 0, this method does not do anything. Throws an ArgumentError if length is negative. Throws an IndexOutOfRangeException if start or start + length - 1 are out of range for this, or if startFrom or startFrom + length - 1 are out of range for from.

void setRange(int start, int length, List<E> from, [int startFrom]);

bool some(bool f(E element)) #

inherited from Collection

Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.

bool some(bool f(E element)) {
  for (E element in this) {
    if (f(element)) return true;
  }
  return false;
}

void sort([Comparator compare = Comparable.compare]) #

Sorts the list according to the order specified by the Comparator.

void sort([Comparator<E> compare = Comparable.compare]);