Skip to content

Arrays

Arrays (also called lists) store multiple values in a single variable. They’re one of the most fundamental data structures in programming.

numbers = []
names = array()
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "orange"]
mixed = [1, "hello", true, 3.14]

Arrays use zero-based indexing (the first element is at index 0):

fruits = ["apple", "banana", "orange"]
firstFruit = fruits[0] // "apple"
secondFruit = fruits[1] // "banana"
thirdFruit = fruits[2] // "orange"

Change an element by assigning to its index:

fruits = ["apple", "banana", "orange"]
fruits[1] = "grape"
// fruits is now ["apple", "grape", "orange"]

Get the number of elements in an array:

numbers = [1, 2, 3, 4, 5]
count = length of numbers // 5
// or
count = numbers.length // 5
numbers = [1, 2, 3]
append 4 to numbers
// or
numbers.append(4)
// or
numbers.push(4)
// numbers is now [1, 2, 3, 4]
fruits = ["apple", "orange"]
insert "banana" at index 1 in fruits
// or
fruits.insert(1, "banana")
// fruits is now ["apple", "banana", "orange"]
numbers = [10, 20, 30, 40]
remove element at index 2 from numbers
// or
numbers.remove(2)
// numbers is now [10, 20, 40]
fruits = ["apple", "banana", "orange"]
if "apple" is in fruits
output "We have apples!"
if "grape" is in fruits
output "We have grapes!"
else
output "No grapes available"
fruits = ["apple", "banana", "orange"]
for each fruit in fruits
output fruit
numbers = [10, 20, 30, 40, 50]
for i from 0 to length of numbers - 1
output "Element {i}: {numbers[i]}"
colors = ["red", "green", "blue"]
i = 0
while i < length of colors
output colors[i]
i++
numbers = [5, 2, 8, 1, 9]
sort numbers in ascending order
// or
numbers.sort()
// numbers is now [1, 2, 5, 8, 9]
numbers = [5, 2, 8, 1, 9]
sort numbers in descending order
// or
numbers.sort(descending)
// numbers is now [9, 8, 5, 2, 1]

Arrays can contain other arrays, creating multi-dimensional structures.

grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Access elements
value = grid[1][2] // Gets 6 (row 1, column 2)
// Tic-tac-toe board
board = [
["X", "O", "X"],
["O", "X", "O"],
["O", "X", "X"]
]
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for each row in grid
for each value in row
output value " "
output "" // New line after each row

Or with indices:

for i from 0 to length of grid - 1
for j from 0 to length of grid[i] - 1
output grid[i][j] " "
output ""
numbers = [1, 2, 3, 4, 5]
sum = 0
for each num in numbers
sum += num
output "Sum: {sum}" // Sum: 15
numbers = [23, 45, 12, 67, 34]
max = numbers[0]
for each num in numbers
if num > max
max = num
output "Maximum: {max}"
numbers = [23, 45, 12, 67, 34]
min = numbers[0]
for each num in numbers
if num < min
min = num
output "Minimum: {min}"
numbers = [1, 2, 3, 2, 1, 2, 4, 2]
target = 2
count = 0
for each num in numbers
if num equals target
count++
output "{target} appears {count} times"
numbers = [1, 2, 3, 4, 5]
reversed = []
for i from length of numbers - 1 to 0
append numbers[i] to reversed
// reversed is now [5, 4, 3, 2, 1]
  • Remember that arrays start at index 0
  • Check array length before accessing elements to avoid errors
  • Use descriptive array names (e.g., studentNames instead of arr1)
  • Arrays can hold mixed types, but it’s often clearer to keep them homogeneous
  • When looping, be careful with the boundary: length of array - 1 is the last valid index