types.Array.swap
Method on every
arraySwaps the elements at index1 and index2.
The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing
... from the prose below, not from the signature.Parameters
index1 | number | index of the first element |
index2 | number | index of the second element |
Return value
This array.
Errors
Throws index out of range unless both indices, after the adjustment described in Notes, are within [0, len()).
Notes
Unlike insert and remove, a negative index here counts back from the end, the same way slice counts its bounds: -1 is the last element.
Example
let a = [10, 20, 30];
a.swap(0, 2);
println("a after swap(0, 2) =", ", ".join(a.map(@(v) v.tostring())));
a.swap(-1, 0); // negative indices wrap, unlike insert() and remove()
println("a after swap(-1, 0) =", ", ".join(a.map(@(v) v.tostring())));
try { a.swap(0, 5) } catch (e) { println("a.swap(0, 5) throws:", e); }Output:
a after swap(0, 2) = 30, 20, 10
a after swap(-1, 0) = 10, 20, 30
a.swap(0, 5) throws: index out of rangeSee also
insert | Inserts value before the element currently at index, shifting the rest up by one. |
remove | Removes the element at index, shifting the rest down by one. |
| array | class index |