Arrays
Arrays (also called lists) store multiple values in a single variable. They’re one of the most fundamental data structures in programming.
Creating Arrays
Section titled “Creating Arrays”Empty Array
Section titled “Empty Array”numbers = []names = array()Array with Initial Values
Section titled “Array with Initial Values”numbers = [1, 2, 3, 4, 5]fruits = ["apple", "banana", "orange"]mixed = [1, "hello", true, 3.14]Accessing Array Elements
Section titled “Accessing Array Elements”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"Modifying Array Elements
Section titled “Modifying Array Elements”Change an element by assigning to its index:
fruits = ["apple", "banana", "orange"]fruits[1] = "grape"// fruits is now ["apple", "grape", "orange"]Array Length
Section titled “Array Length”Get the number of elements in an array:
numbers = [1, 2, 3, 4, 5]count = length of numbers // 5// orcount = numbers.length // 5Adding Elements
Section titled “Adding Elements”Append to End
Section titled “Append to End”numbers = [1, 2, 3]append 4 to numbers// ornumbers.append(4)// ornumbers.push(4)// numbers is now [1, 2, 3, 4]Insert at Specific Position
Section titled “Insert at Specific Position”fruits = ["apple", "orange"]insert "banana" at index 1 in fruits// orfruits.insert(1, "banana")// fruits is now ["apple", "banana", "orange"]Removing Elements
Section titled “Removing Elements”Remove by Index
Section titled “Remove by Index”numbers = [10, 20, 30, 40]remove element at index 2 from numbers// ornumbers.remove(2)// numbers is now [10, 20, 40]Checking for Elements
Section titled “Checking for Elements”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"Iterating Through Arrays
Section titled “Iterating Through Arrays”For Each Loop
Section titled “For Each Loop”fruits = ["apple", "banana", "orange"]
for each fruit in fruits output fruitIndex-Based Loop
Section titled “Index-Based Loop”numbers = [10, 20, 30, 40, 50]
for i from 0 to length of numbers - 1 output "Element {i}: {numbers[i]}"While Loop
Section titled “While Loop”colors = ["red", "green", "blue"]i = 0
while i < length of colors output colors[i] i++Sorting Arrays
Section titled “Sorting Arrays”Ascending Order
Section titled “Ascending Order”numbers = [5, 2, 8, 1, 9]sort numbers in ascending order// ornumbers.sort()// numbers is now [1, 2, 5, 8, 9]Descending Order
Section titled “Descending Order”numbers = [5, 2, 8, 1, 9]sort numbers in descending order// ornumbers.sort(descending)// numbers is now [9, 8, 5, 2, 1]Multi-Dimensional Arrays
Section titled “Multi-Dimensional Arrays”Arrays can contain other arrays, creating multi-dimensional structures.
2D Array (Matrix)
Section titled “2D Array (Matrix)”grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Access elementsvalue = grid[1][2] // Gets 6 (row 1, column 2)Creating a 2D Array
Section titled “Creating a 2D Array”// Tic-tac-toe boardboard = [ ["X", "O", "X"], ["O", "X", "O"], ["O", "X", "X"]]Iterating Through 2D Arrays
Section titled “Iterating Through 2D Arrays”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 rowOr 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 ""Common Array Operations
Section titled “Common Array Operations”Sum of Array
Section titled “Sum of Array”numbers = [1, 2, 3, 4, 5]sum = 0
for each num in numbers sum += num
output "Sum: {sum}" // Sum: 15Find Maximum
Section titled “Find Maximum”numbers = [23, 45, 12, 67, 34]
max = numbers[0]for each num in numbers if num > max max = num
output "Maximum: {max}"Find Minimum
Section titled “Find Minimum”numbers = [23, 45, 12, 67, 34]
min = numbers[0]for each num in numbers if num < min min = num
output "Minimum: {min}"Count Occurrences
Section titled “Count Occurrences”numbers = [1, 2, 3, 2, 1, 2, 4, 2]target = 2count = 0
for each num in numbers if num equals target count++
output "{target} appears {count} times"Reverse Array
Section titled “Reverse Array”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]Array Tips
Section titled “Array Tips”- Remember that arrays start at index 0
- Check array length before accessing elements to avoid errors
- Use descriptive array names (e.g.,
studentNamesinstead ofarr1) - Arrays can hold mixed types, but it’s often clearer to keep them homogeneous
- When looping, be careful with the boundary:
length of array - 1is the last valid index