Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arrays

Arrays are ordered, mutable collections that can hold any mix of types.

Creating arrays

let items = [1, 2, 3]
let empty = []
let mixed = [1, "two", true, none]

Accessing elements

Arrays are 0-indexed. Negative indices count from the end:

let items = [10, 20, 30]
print(items[0])     // 10
print(items[2])     // 30
print(items[-1])    // 30 (last element)
print(items[-2])    // 20

Out-of-bounds access produces an error.

Modifying elements

let items = [10, 20, 30]
items[0] = 99
print(items)    // [99, 20, 30]

Methods

MethodReturnsDescription
arr.len()numberNumber of elements
arr.push(val)noneAppend to end
arr.pop()valueRemove and return last element
arr.has(val)boolWhether the array contains the value
arr.index_of(val)number or noneIndex of first occurrence, or none
arr.insert(i, val)noneInsert at index, shifting elements right
arr.remove(i)valueRemove at index, shifting elements left
arr.slice(start, end)arrayNew sub-array (both args optional)
arr.reverse()arrayReverse in place, returns the array
arr.sort()arraySort in place, returns the array
arr.join(sep)stringJoin elements into a string

Practical examples

Building a list

let squares = []
for i in range(1, 6) {
  squares.push(i * i)
}
print(squares)    // [1, 4, 9, 16, 25]

Filtering values

let numbers = [3, 7, 1, 9, 4, 6, 2, 8]
let big = []
for n in numbers {
  if n > 5 {
    big.push(n)
  }
}
print(big)    // [7, 9, 6, 8]

Checking membership

let allowed = ["admin", "editor", "viewer"]
let role = "editor"
if allowed.has(role) {
  print("Access granted")
} else {
  print("Access denied")
}

Sorting and joining

let scores = [42, 17, 85, 3]
scores.sort()
print(scores)    // [3, 17, 42, 85]

let names = ["Charlie", "Alice", "Bob"]
names.sort()
print(names.join(", "))    // "Alice, Bob, Charlie"