Arrays
What is an array?
Imagine you have a list of groceries: eggs, milk, bread, butter. You could create a separate variable for each item, but that gets messy fast. An array is a single variable that holds a list of values in order.
Think of an array like a row of numbered boxes. Each box has a position (starting from 0) and contains one value.
Position: 0 1 2 3
[eggs] [milk] [bread] [butter]
Creating arrays
In Nyx, you create an array with square brackets:
fn main() { let numbers: Array = [10, 20, 30, 40, 50] let names: Array = ["Alice", "Bob", "Charlie"] let empty: Array = [] print(numbers) print(names) print(empty) }
Arrays can hold any type of value: integers, strings, booleans, or even other arrays.
Accessing elements
You access an element by its index — its position in the array. Indices start at 0, not 1:
fn main() { let fruits: Array = ["apple", "banana", "cherry", "date"] print(fruits[0]) // apple (first element) print(fruits[1]) // banana (second element) print(fruits[3]) // date (fourth element) }
Why start at 0? It is a convention in most programming languages. The first element is at position 0, the second at position 1, and so on.
Modifying elements
If you declare an array with var, you can change its elements:
fn main() { var scores: Array = [85, 90, 78] print(scores[1]) // 90 scores[1] = 95 print(scores[1]) // 95 }
Array length
The .length() method tells you how many elements an array has:
fn main() { let colors: Array = ["red", "green", "blue"] print(colors.length()) // 3 let empty: Array = [] print(empty.length()) // 0 }
Adding elements
The .push() method adds an element to the end of an array:
fn main() { var list: Array = [1, 2, 3] print(list.length()) // 3 list.push(4) list.push(5) print(list.length()) // 5 print(list[4]) // 5 }
Removing the last element
The .pop() method removes and returns the last element:
fn main() { var stack: Array = [10, 20, 30] let last: int = stack.pop() print(last) // 30 print(stack.length()) // 2 }
Inserting and removing at any position
The .insert(index, value) method adds an element at a specific position, shifting elements to the right. The .remove(index) method removes and returns the element at a specific position, shifting elements to the left:
fn main() { var nums: Array = [10, 30, 40] nums.insert(1, 20) // insert 20 at index 1 print(nums) // [10, 20, 30, 40] let removed: int = nums.remove(2) // remove element at index 2 print(removed) // 30 print(nums) // [10, 20, 40] }
These methods are more flexible than push/pop, which only work at the end. Use them when you need to add or remove elements in the middle of an array.
Looping through arrays
The most common thing to do with an array is to go through every element. Use a while loop with an index:
fn main() { let names: Array = ["Alice", "Bob", "Charlie", "Diana"] var i: int = 0 while i < names.length() { print(names[i]) i += 1 } }
Output:
Alice Bob Charlie Diana
Practical example: finding the maximum
Let's write a function that finds the largest number in an array:
fn find_max(arr: Array) -> int { var max_val: int = arr[0] var i: int = 1 while i < arr.length() { if arr[i] > max_val { max_val = arr[i] } i += 1 } return max_val } fn main() { let scores: Array = [72, 95, 88, 61, 100, 84] print(find_max(scores)) // 100 }
Practical example: sum of all elements
fn sum(arr: Array) -> int { var total: int = 0 var i: int = 0 while i < arr.length() { total += arr[i] i += 1 } return total } fn main() { let numbers: Array = [10, 20, 30, 40] print(sum(numbers)) // 100 }
Practical example: counting occurrences
fn count_value(arr: Array, target: int) -> int { var count: int = 0 var i: int = 0 while i < arr.length() { if arr[i] == target { count += 1 } i += 1 } return count } fn main() { let data: Array = [1, 3, 5, 3, 7, 3, 9] print(count_value(data, 3)) // 3 print(count_value(data, 7)) // 1 print(count_value(data, 2)) // 0 }
Exercises
- Write a function
find_min(arr: Array) -> intthat returns the smallest element.
- Write a function
average(arr: Array) -> intthat returns the integer average of all elements.
- Write a function
reverse_array(arr: Array) -> Arraythat returns a new array with elements in reverse order. Hint: create an empty array, loop from the last element to the first, and push each one.
- Write a program that creates an array of the first 20 even numbers (2, 4, 6, ..., 40) using a loop with
.push(), then prints them all.
- Write a function
contains(arr: Array, target: int) -> boolthat returnstrueif the array contains the target value.
Summary
- Arrays hold ordered lists of values:
let a: Array = [1, 2, 3] - Access elements by index (starting at 0):
a[0] .length()returns the number of elements..push(val)adds to the end..pop()removes from the end.- Loop through arrays with
while i < arr.length(). - Arrays are one of the most fundamental data structures in programming.
Next chapter: Strings →