Is list same as array in Dart?

An indexable collection of objects with a length.

Subclasses of this class implement different kinds of lists. The most common kinds of lists are:

  • Fixed-length list. An error occurs when attempting to use operations that can change the length of the list.

  • Growable list. Full implementation of the API defined in this class.

The default growable list, as created by [], keeps an internal buffer, and grows that buffer when necessary. This guarantees that a sequence of add operations will each execute in amortized constant time. Setting the length directly may take time proportional to the new length, and may change the internal capacity so that a following add operation will need to immediately increase the buffer capacity. Other list implementations may have different performance behavior.

The following code illustrates that some List implementations support only a subset of the API.

var fixedLengthList = List.filled[5, 0]; fixedLengthList.length = 0; // Error fixedLengthList.add[499]; // Error fixedLengthList[0] = 87; var growableList = [1, 2]; growableList.length = 0; growableList.add[499]; growableList[0] = 87;

Lists are Iterable. Iteration occurs over values in index order. Changing the values does not affect iteration, but changing the valid indicesthat is, changing the list's lengthbetween iteration steps causes a ConcurrentModificationError. This means that only growable lists can throw ConcurrentModificationError. If the length changes temporarily and is restored before continuing the iteration, the iterator might not detect it.

It is generally not allowed to modify the list's length [adding or removing elements] while an operation on the list is being performed, for example during a call to forEach or sort. Changing the list's length while it is being iterated, either by iterating it directly or through iterating an Iterable that is backed by the list, will break the iteration.

Implemented typesImplementersAvailable Extensions

Constructors

[[int? length]]Creates a list of the given length. [...]List.empty[{bool growable = false}]Creates a new empty list. [...]List.filled[int length, E fill, {bool growable = false}]Creates a list of the given length with fill at each position. [...]List.from[Iterable elements, {bool growable = true}]Creates a list containing all elements. [...]List.generate[int length, E generator[int index], {bool growable = true}]Generates a list of values. [...]List.of[Iterable elements, {bool growable = true}]Creates a list from elements. [...]List.unmodifiable[Iterable elements]Creates an unmodifiable list containing all elements. [...]

Properties

first EReturns the first element. [...]

read / write, inherited-getter

hashCode intThe hash code for this object. [...]isEmpty boolWhether this collection has no elements. [...]isNotEmpty boolWhether this collection has at least one element. [...]iterator IteratorReturns a new Iterator that allows iterating the elements of this Iterable. [...]last EReturns the last element. [...]

read / write, inherited-getter

length intThe number of objects in this list. [...]reversed IterableAn Iterable of the objects in this list in reverse order.A representation of the runtime type of the object.single EChecks that this iterable has only one element, and returns that element. [...]

Methods

add[E value] voidAdds value to the end of this list, extending the length by one. [...]addAll[Iterable iterable] voidAppends all objects of iterable to the end of this list. [...]any[bool test[E element]] boolChecks whether any element of this iterable satisfies test. [...]asMap[] MapAn unmodifiable Map view of this list. [...]cast[] ListReturns a view of this list as a list of R instances. [...]clear[] voidRemoves all objects from this list; the length of the list becomes zero. [...]contains[Object? element] boolWhether the collection contains an element equal to element. [...]elementAt[int index] EReturns the indexth element. [...]every[bool test[E element]] boolChecks whether every element of this iterable satisfies test. [...]expand[Iterable toElements[E element]] IterableExpands each element of this Iterable into zero or more elements. [...]fillRange[int start, int end, [E? fillValue]] voidOverwrites a range of elements with fillValue. [...]firstWhere[bool test[E element], {E orElse[]?}] EReturns the first element that satisfies the given predicate test. [...]fold[T initialValue, T combine[T previousValue, E element]] TReduces a collection to a single value by iteratively combining each element of the collection with an existing value [...]followedBy[Iterable other] IterableReturns the lazy concatenation of this iterable and other. [...]forEach[void action[E element]] voidInvokes action on each element of this iterable in iteration order.getRange[int start, int end] IterableCreates an Iterable that iterates over a range of elements. [...]indexOf[E element, [int start = 0]] intThe first index of element in this list. [...]indexWhere[bool test[E element], [int start = 0]] intThe first index in the list that satisfies the provided test. [...]insert[int index, E element] voidInserts element at position index in this list. [...]insertAll[int index, Iterable iterable] voidInserts all objects of iterable at position index in this list. [...]join[[String separator = ""]] StringConverts each element to a String and concatenates the strings. [...]lastIndexOf[E element, [int? start]] intThe last index of element in this list. [...]lastIndexWhere[bool test[E element], [int? start]] intThe last index in the list that satisfies the provided test. [...]lastWhere[bool test[E element], {E orElse[]?}] EReturns the last element that satisfies the given predicate test. [...]map[T toElement[E e]] IterableThe current elements of this iterable modified by toElement. [...]Invoked when a non-existent method or property is accessed. [...]reduce[E combine[E value, E element]] EReduces a collection to a single value by iteratively combining elements of the collection using the provided function. [...]remove[Object? value] boolRemoves the first occurrence of value from this list. [...]removeAt[int index] ERemoves the object at position index from this list. [...]removeLast[] ERemoves and returns the last object in this list. [...]removeRange[int start, int end] voidRemoves a range of elements from the list. [...]removeWhere[bool test[E element]] voidRemoves all objects from this list that satisfy test. [...]replaceRange[int start, int end, Iterable replacements] voidReplaces a range of elements with the elements of replacements. [...]retainWhere[bool test[E element]] voidRemoves all objects from this list that fail to satisfy test. [...]setAll[int index, Iterable iterable] voidOverwrites elements with the objects of iterable. [...]setRange[int start, int end, Iterable iterable, [int skipCount = 0]] voidWrites some elements of iterable into a range of this list. [...]shuffle[[Random? random]] voidShuffles the elements of this list randomly.singleWhere[bool test[E element], {E orElse[]?}] EReturns the single element that satisfies test. [...]skip[int count] IterableReturns an Iterable that provides all but the first count elements. [...]skipWhile[bool test[E value]] IterableReturns an Iterable that skips leading elements while test is satisfied. [...]sort[[int compare[E a, E b]?]] voidSorts this list according to the order specified by the compare function. [...]sublist[int start, [int? end]] ListReturns a new list containing the elements between start and end. [...]take[int count] IterableReturns a lazy iterable of the count first elements of this iterable. [...]takeWhile[bool test[E value]] IterableReturns a lazy iterable of the leading elements satisfying test. [...]toList[{bool growable = true}] ListCreates a List containing the elements of this Iterable. [...]toSet[] SetCreates a Set containing the same elements as this iterable. [...]toString[] StringA string representation of this object. [...]where[bool test[E element]] IterableReturns a new lazy Iterable with all elements that satisfy the predicate test. [...]whereType[] IterableReturns a new lazy Iterable with all elements that have type T. [...]

Operators

operator +[List other] ListReturns the concatenation of this list and other. [...]operator ==[Object other] boolWhether this list is equal to other. [...]operator [][int index] EThe object at the given index in the list. [...]operator []=[int index, E value] voidSets the value at the given index in the list to value. [...]

Static Methods

castFrom[List source] ListAdapts source to be a List. [...]copyRange[List target, int at, List source, [int? start, int? end]] voidCopy a range of one list into another list. [...]writeIterable[List target, int at, Iterable source] voidWrite the elements of an iterable into a list. [...]

Video liên quan

Chủ Đề