Arrays
An array holds multiple values of the same type in a sequence. Each value is accessed by its index (position).
Declaring an Array
Section titled “Declaring an Array”Use Type[] syntax:
int[] numbers;numbers = [10, 20, 30, 40];
string[] names = ["John", "Alice", "Bob"];Default Value
Section titled “Default Value”An uninitialized array variable starts at null. You must assign it before
use:
int[] items;// print(items[0]); ERROR, array is null
items = [1, 2, 3]; // now it's readyAccessing Elements
Section titled “Accessing Elements”Use square brackets with the index. Indices start at 0:
int[] arr = [10, 20, 30];
print(arr[0]); // 10print(arr[1]); // 20print(arr[2]); // 30
arr[1] = 99; // change value at index 1print(arr[1]); // 99Out of Bounds
Section titled “Out of Bounds”Accessing an index that doesn’t exist causes a runtime error that can be
caught with try/catch:
int[] arr = [1, 2, 3];
try { print(arr[99]); // index out of bounds} catch (Error e) { print(e.code);}Reference Semantics
Section titled “Reference Semantics”Arrays are reference types: assigning one array to another shares the same data:
int[] a = [1, 2, 3];int[] b = a; // b refers to the same array
b[0] = 99;print(a[0]); // 99, a changed too!Built-in Array Methods
Section titled “Built-in Array Methods”Arrays come with several built-in methods. Use dot syntax to call them:
int[] arr = [10, 20, 30];
int len = arr.length(); // 3, number of elementsarr.push(40); // add to end → [10, 20, 30, 40]int last = arr.pop(); // remove end → 40, arr is [10, 20, 30]arr.reverse(); // reverse in-place → [30, 20, 10]Full list:
| Method | Returns | Description |
|---|---|---|
arr.length() |
int |
Number of elements |
arr.push(val) |
int |
Add to end (returns new index) |
arr.pop() |
E |
Remove and return last element |
arr.insert(index, val) |
int |
Insert at position (returns index) |
arr.remove(index) |
E |
Remove and return element at index |
arr.slice(start, end) |
E[] |
New array from range [start, end) |
arr.reverse() |
E[] |
Reverse in-place (same reference returned) |
arr.concat(other) |
E[] |
New array combining both |
arr.contains(val) |
bool |
Check if value exists |
arr.indexOf(val) |
int? |
Find index (returns null if not found) |
arr.clear() |
void |
Remove all elements |
Some of these methods (
push,pop,insert,remove,reverse,clear) change the array in place, and because arrays are shared references, that change is visible through every variable pointing at the same array. See the Built-in Functions page for a from-scratch walkthrough of each method, including which ones mutate.
Looping Through Arrays
Section titled “Looping Through Arrays”string[] names = ["John", "Alice", "Bob"];
for (int i = 0; i < 3; i = i + 1) { print(names[i]);}// Output: John Alice BobArrays with Structs
Section titled “Arrays with Structs”You can store structs in arrays:
struct Person { string name; int age;}
int main() { Person p1; p1.name = "John"; p1.age = 30;
Person p2; p2.name = "Alice"; p2.age = 25;
Person[] people = [p1, p2];
print(people[0].name); // "John" print(people[1].age); // 25
people[0].age = 31; // modify through array return 0;}Arrays of Nullable Types
Section titled “Arrays of Nullable Types”int?[] values = [10, null, 30, null, 50];
for (int i = 0; i < 5; i = i + 1) { if (values[i] != null) { print(values[i]); }}// Output: 10 30 50Common Patterns
Section titled “Common Patterns”Sum all elements
Section titled “Sum all elements”int sum(int[] arr) { int total = 0; for (int i = 0; i < arr.length(); i = i + 1) { total = total + arr[i]; } return total;}Find max
Section titled “Find max”int findMax(int[] arr) { int result = arr[0]; for (int i = 1; i < arr.length(); i = i + 1) { if (arr[i] > result) { result = arr[i]; } } return result;}What’s Next?
Section titled “What’s Next?”- Work with text using strings
- Handle errors with try/catch/throw
