2015-01-18 15:36:36 -08:00
^title List Class
^category core
Extends [Sequence ](sequence.html ).
2015-01-22 20:58:22 -08:00
An indexable contiguous collection of elements. More details [here ](../lists.html ).
2015-03-27 07:43:36 -07:00
## Methods
2015-01-18 15:36:36 -08:00
### **add**(item)
2015-01-25 11:08:13 -08:00
Appends `item` to the end of the list.
2015-01-18 15:36:36 -08:00
2015-02-26 23:08:36 -08:00
### **clear**()
2015-01-18 15:36:36 -08:00
2015-03-14 14:17:21 +01:00
Removes all elements from the list.
2015-01-18 15:36:36 -08:00
### **count**
2015-03-14 14:17:21 +01:00
The number of elements in the list.
2015-01-18 15:36:36 -08:00
Reverse the argument order of List.insert
The previous order, insert(element, index), was counter-intuitive.
I'm not aware of any list API that uses this order. I've checked:
* Ruby Array.insert(index, obj...)
* JavaScript array.splice(start, deleteCount[, item1[, item2[, ...]]])
* C++ / QList::insert(int i, const T & value)
* C++ / std::vector::insert
* Lua table.insert (list, [pos,] value)
* C# List<T>.Insert(int index, T item)
* Java Interface List<E>.add(int index, E element)
* Python list.insert(i, x)
So it seemed to me more like an oversight in Wren.
2015-03-15 22:51:24 +01:00
### **insert**(index, item)
2015-01-18 15:36:36 -08:00
2015-04-22 07:45:20 -07:00
Inserts the `item` at `index` in the list.
2015-04-04 15:23:16 -07:00
:::dart
var list = ["a", "b", "c", "d"]
list.insert(1, "e")
2015-09-15 07:46:09 -07:00
System.print(list) // "[a, e, b, c, d]".
2015-04-04 15:23:16 -07:00
2015-04-22 07:45:20 -07:00
The `index` may be one past the last index in the list to append an element.
:::dart
var list = ["a", "b", "c"]
list.insert(3, "d")
2015-09-15 07:46:09 -07:00
System.print(list) // "[a, b, c, d]".
2015-04-22 07:45:20 -07:00
If `index` is negative, it counts backwards from the end of the list. It bases this on the length of the list *after* inserted the element, so that `-1` will append the element, not insert it before the last element.
:::dart
var list = ["a", "b"]
list.insert(-1, "d")
list.insert(-2, "c")
2015-09-15 07:46:09 -07:00
System.print(list) // "[a, b, c, d]".
2015-04-22 07:45:20 -07:00
2015-04-04 15:23:16 -07:00
Returns the inserted item.
:::dart
2015-09-15 07:46:09 -07:00
System.print(["a", "c"].insert(1, "b")) // "b".
2015-04-04 15:23:16 -07:00
It is a runtime error if the index is not an integer or is out of bounds.
2015-01-18 15:36:36 -08:00
### **iterate**(iterator), **iteratorValue**(iterator)
2015-01-22 20:58:22 -08:00
Implements the [iterator protocol ](../control-flow.html#the-iterator-protocol )
for iterating over the elements in the list.
2015-01-18 15:36:36 -08:00
### **removeAt**(index)
2015-01-25 11:08:13 -08:00
Removes the element at `index` . If `index` is negative, it counts backwards
from the end of the list where `-1` is the last element. All trailing elements
are shifted up to fill in where the removed element was.
:::dart
var list = ["a", "b", "c", "d"]
list.removeAt(1)
2015-09-15 07:46:09 -07:00
System.print(list) // "[a, c, d]".
2015-01-25 11:08:13 -08:00
2015-02-22 10:26:31 -08:00
Returns the removed item.
2015-09-15 07:46:09 -07:00
System.print(["a", "b", "c"].removeAt(1)) // "b".
2015-02-22 10:26:31 -08:00
2015-01-25 11:08:13 -08:00
It is a runtime error if the index is not an integer or is out of bounds.
2015-01-18 15:36:36 -08:00
### **[**index**]** operator
2015-01-25 11:08:13 -08:00
Gets the element at `index` . If `index` is negative, it counts backwards from
the end of the list where `-1` is the last element.
:::dart
var list = ["a", "b", "c"]
2015-09-15 07:46:09 -07:00
System.print(list[1]) // "b".
2015-01-25 11:08:13 -08:00
It is a runtime error if the index is not an integer or is out of bounds.
2015-01-18 15:36:36 -08:00
### **[**index**]=**(item) operator
2015-01-25 11:08:13 -08:00
Replaces the element at `index` with `item` . If `index` is negative, it counts
backwards from the end of the list where `-1` is the last element.
:::dart
var list = ["a", "b", "c"]
list[1] = "new"
2015-09-15 07:46:09 -07:00
System.print(list) // "[a, new, c]".
2015-01-25 11:08:13 -08:00
It is a runtime error if the index is not an integer or is out of bounds.