Array
An ordered, random-access collection that can grow dynamically, with elements of a single type.
var xs: [Int] = [1, 2, 3]
Accessing values
var xs = [1, 2, 3]
// Get first element
var first = xs[0]
// Write to second element
xs[1] = 42
// Iterate over elements
for element in xs {
print("\{element}")
}
// Iterate over elements with both index and value retrieval
for i, element in xs.enumerated() {
print("\{i}, \{element}")
}
Printing
Arrays can be interpolated into strings and print their contents.
var xs = [1, 2, 3]
print("\{xs}") // Prints [1, 2, 3]
Copying an Array
The array type has reference semantics. Passing an array to a function allows it to mutate it. To copy an array use the copy method.
fun change(_ data: [Int]) {
data.append(5)
}
var original = [1, 2]
change(original) // changes original
print("\{original}") // prints [1, 2, 5]
var not_a_copy = original // doesn't make a copy
not_a_copy.append(10)
print("\{original}") // prints [1, 2, 5, 10]
var a_copy = original.copy()
a_copy.append(42)
print("\{original}") // prints [1, 2, 5, 10]
Properties
first: Element? (get)
The first element in the array if there is any.
is_empty: Bool (get)
Whether the array contains no elements.
last: Element? (get)
The last element in the array if there is any.
length: Int (get)
Number of elements in the array.
Methods
all_satisfy(_ predicate: (Element) -> (Bool)) -> (Bool)
Returns whether all elements satisfy the predicate
.
Returns
true
on an empty array.
append(_ element: Element)
Appends element
to the end of the array.
append_all(_ other: [Element])
Appends all elements from other
.
clear()
Removes all elements.
contains(_ el: Element) -> (Bool)
Returns whether the given element is found in the array.
Requirements
This method is only available if Element
supports ==
.
copy() -> ([Element])
Returns a copy of the array.
enumerated() -> (Enumerated<Element>)
Returns an enumerated sequence over the array with an iterator which provides both the index and the value.
filtered(by should_be_included: (Element) -> (Bool)) -> ([Element])
Returns a new array filtered to the elements that should_be_included
returned true
for.
first_match(where: (Element) -> (Bool)) -> (Element?)
Returns the first element that matches if there is any.
index(of element: Element) -> (Int?)
Returns the first index of the element if it exists.
Requirements
This method is only available if Element
supports ==
.
insert(_ element: Element, at: Int)
Inserts element
at the specified index.
joined(separator: String) -> (String)
Combines all elements into a single string representation separating each element with separator
.
Requirements
This method is only available if the elements can be converted to string.
last_index(of element: Element) -> (Int?)
Returns the last index of the element if it exists.
Requirements
This method is only available if Element
supports ==
.
last_match(where: (Element) -> (Bool)) -> (Element?)
Returns the last element that matches if there is any.
pop_last() -> (Element?)
Removes the last element and returns it.
remove(at index: Int) -> Element
Removes the element at index
and returns it.
remove_all(where predicate: (Element) -> (Bool))
Removes all elements that match.
reverse()
Reverse the elements in place.
reversed() -> ([Element])
Returns a new array with the elements in reverse order.
some_satisfy(_ predicate: (Element) -> (Bool)) -> (Bool)
Returns whether there's at least one element matching predicate
.
sort()
Sorts the elements in ascending order in place
Requirements
This method is only available if Element
supports <
.
sorted() -> ([Element])
Returns a new array with the elements sorted in ascending order.
Requirements
This method is only available if Element
supports <
.
sort_by(_ predicate: (Element, Element) -> (Bool))
Sorts the elements in place using predicate
.
The predicate
should return whether the first element should be sorted before the second element.
sorted_by(_ predicate: (Element, Element) -> (Bool))
Returns a new array with the elements sorted using predicate
.
The predicate
should return whether the first element should be sorted before the second element.
subsequence(from: Int, length: Int? = nil) -> [Element]
Returns a new array containing the elements in the specified range.
Parameters
from
The starting index
count
The number of elements to include after from
. If not provided it will include the remainder after from
.