2014-01-30 06:51:52 -08:00
|
|
|
^title Lists
|
|
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
A list is a compound object that holds a collection of elements identified by
|
|
|
|
|
integer index. You can create a list by placing a sequence of comma-separated
|
|
|
|
|
expressions inside square brackets:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
[1, "banana", true]
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
Here, we've created a list of three elements. Notice that the elements don't
|
|
|
|
|
have to be the same type.
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2014-04-07 21:03:16 -07:00
|
|
|
## Accessing elements
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
You can access an element from a list by calling the [subscript
|
2015-11-07 11:09:04 -08:00
|
|
|
operator][] on it with the index of the
|
2015-01-03 23:27:02 -08:00
|
|
|
element you want. Like most languages, indexes start at zero:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-11-07 11:09:04 -08:00
|
|
|
[subscript operator]: method-calls.html#subscripts
|
|
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
var hirsute = ["sideburns", "porkchops", "'stache", "goatee"]
|
|
|
|
|
System.print(hirsute[0]) //> sideburns
|
|
|
|
|
System.print(hirsute[1]) //> porkchops
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
|
|
|
|
Negative indices counts backwards from the end:
|
|
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print(hirsute[-1]) //> goatee
|
|
|
|
|
System.print(hirsute[-2]) //> 'stache
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
It's a runtime error to pass an index outside of the bounds of the list. If you
|
|
|
|
|
don't know what those bounds are, you can find out using count:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print(hirsute.count) //> 4
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-01 18:48:59 -08:00
|
|
|
## Slices and ranges
|
|
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
Sometimes you want to copy a chunk of elements from a list. You can do that by
|
|
|
|
|
passing a [range](values.html#ranges) to the subscript operator, like so:
|
2015-01-01 18:48:59 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print(hirsute[1..2]) //> [porkchops, 'stache]
|
|
|
|
|
</pre>
|
2015-01-01 18:48:59 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
This returns a new list containing the elements of the original list whose
|
|
|
|
|
indices are within the given range. Both inclusive and exclusive ranges work
|
|
|
|
|
and do what you expect.
|
2015-01-01 18:48:59 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
Negative bounds also work like they do when passing a single number, so to copy
|
|
|
|
|
a list, you can just do:
|
2015-01-01 18:48:59 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
hirsute[0..-1]
|
|
|
|
|
</pre>
|
2015-01-01 18:48:59 -08:00
|
|
|
|
2014-01-31 17:51:09 -08:00
|
|
|
## Adding elements
|
|
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
Lists are *mutable*, meaning their contents can be changed. You can swap out an
|
|
|
|
|
existing element in the list using the subscript setter:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
hirsute[1] = "muttonchops"
|
|
|
|
|
System.print(hirsute[1]) //> muttonchops
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
It's an error to set an element that's out of bounds. To grow a list, you can
|
|
|
|
|
use `add` to append a single item to the end:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
hirsute.add("goatee")
|
|
|
|
|
System.print(hirsute.count) //> 5
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
|
|
|
|
You can insert a new element at a specific position using `insert`:
|
|
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
hirsute.insert(2, "soul patch")
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -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
|
|
|
The first argument is the index to insert at, and the second is the value to
|
|
|
|
|
insert. All elements following the inserted one will be pushed down to
|
2015-01-03 23:27:02 -08:00
|
|
|
make room for it.
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
It's valid to "insert" after the last element in the list, but only *right*
|
|
|
|
|
after it. Like other methods, you can use a negative index to count from the
|
|
|
|
|
back. Doing so counts back from the size of the list *after* it's grown by one:
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
var letters = ["a", "b", "c"]
|
|
|
|
|
letters.insert(3, "d") // OK: inserts at end.
|
|
|
|
|
System.print(letters) //> [a, b, c, d]
|
|
|
|
|
letters.insert(-2, "e") // Counts back from size after insert.
|
|
|
|
|
System.print(letters) //> [a, b, c, e, d]
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2019-02-05 18:18:38 -08:00
|
|
|
## Adding lists together
|
|
|
|
|
|
|
|
|
|
Lists have the ability to be added together via the `+` operator. This is often known as concatenation.
|
|
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
var letters = ["a", "b", "c"]
|
|
|
|
|
var other = ["d", "e", "f"]
|
|
|
|
|
var combined = letters + other
|
|
|
|
|
System.print(combined) //> [a, b, c, d, e, f]
|
|
|
|
|
</pre>
|
2019-02-05 18:18:38 -08:00
|
|
|
|
2014-01-31 17:51:09 -08:00
|
|
|
## Removing elements
|
|
|
|
|
|
2015-01-03 23:27:02 -08:00
|
|
|
The opposite of `insert` is `removeAt`. It removes a single element from a
|
2021-04-03 19:55:42 -07:00
|
|
|
given position in the list.
|
|
|
|
|
|
|
|
|
|
To remove a specific _value_ instead, use `remove`. The first value that
|
|
|
|
|
matches using regular equality will be removed.
|
|
|
|
|
|
|
|
|
|
In both cases, all following items are shifted up to fill in the gap.
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
var letters = ["a", "b", "c", "d"]
|
|
|
|
|
letters.removeAt(1)
|
|
|
|
|
System.print(letters) //> [a, c, d]
|
2021-04-03 19:55:42 -07:00
|
|
|
letters.remove("a")
|
|
|
|
|
System.print(letters) //> [c, d]
|
2020-06-05 14:57:20 -07:00
|
|
|
</pre>
|
2015-02-22 10:26:31 -08:00
|
|
|
|
2021-04-03 19:55:42 -07:00
|
|
|
Both the `remove` and `removeAt` method return the removed item:
|
2015-02-22 10:26:31 -08:00
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print(letters.removeAt(1)) //> c
|
|
|
|
|
</pre>
|
2014-01-31 17:51:09 -08:00
|
|
|
|
2021-04-03 19:55:42 -07:00
|
|
|
If `remove` couldn't find the value in the list, it returns null:
|
|
|
|
|
|
|
|
|
|
<pre class="snippet">
|
|
|
|
|
System.print(letters.remove("not found")) //> null
|
|
|
|
|
</pre>
|
|
|
|
|
|
2014-01-31 17:51:09 -08:00
|
|
|
If you want to remove everything from the list, you can clear it:
|
|
|
|
|
|
2020-06-05 14:57:20 -07:00
|
|
|
<pre class="snippet">
|
|
|
|
|
hirsute.clear()
|
|
|
|
|
System.print(hirsute) //> []
|
|
|
|
|
</pre>
|
2015-11-07 11:09:04 -08:00
|
|
|
|
2019-02-05 18:18:38 -08:00
|
|
|
<br><hr>
|
2015-11-07 11:09:04 -08:00
|
|
|
<a class="right" href="maps.html">Maps →</a>
|
|
|
|
|
<a href="values.html">← Values</a>
|