Collections, 1 of 6
Arrays
Make a list that grows, change it, copy it, and use fixed-size arrays for data of a set size.
What you’ll build
A program that keeps a class’s test scores: it adds and removes scores, sorts them, and finds the best. Then it lays out a seating plan in a fixed-size grid. Along the way you’ll see the difference between an array that grows and one whose size never changes.
A list that grows
An array literal, values in brackets, makes a T[]: an array of values of type T, which can grow. Add
appends a value, Insert puts one at an index, and RemoveAt takes one out. Count is how many
there are, and array[i] is the value at index i, counting from 0.
Import std.core
Local scores = [72, 95, 88]
scores.Add(61)
scores.Insert(0, 100)
Print(scores, scores.Count)
scores.RemoveAt(1)
Print(scores)
Print("first {scores[0]}, last {scores[scores.Count - 1]}")
Print(scores.Contains(95), scores.Contains(50))[100, 72, 95, 88, 61] 5 [100, 95, 88, 61] first 100, last 61 True False
An empty array has no values to give it a type, so [] needs one from where it’s used, as in
Local names: String[] = [].
Sorting and searching
std.collections gives every array more Methods: Sort puts the values in order, IndexOf finds
one, Filter keeps those that pass a test, and many more. Each Method that can come up empty,
like First or IndexOf, gives a value that can be Null, which ?? replaces.
Import std.core
Import std.collections
Local scores = [72, 95, 88, 61, 79]
scores.Sort()
Print(scores)
Print("best {scores.Last() ?? 0}, lowest {scores.First() ?? 0}")
Print("88 is at {scores.IndexOf(88) ?? -1}")
Local passed = scores.Filter(Function(s) => s >= 75)
Print("{passed.Count} passed: {passed}")
Print("total {scores.Fold(0, Function(sum, s) => sum + s)}")[61, 72, 79, 88, 95] best 95, lowest 61 88 is at 3 3 passed: [79, 88, 95] total 395
Sort() sorts in place, and changes the array it’s called on. Filter makes a new array.
Arrays are shared
A T[] is a reference: assigning it to another variable gives a second name for the same array,
not a copy, and a change through either shows through both. Clone makes a copy.
Import std.core
Local scores = [72, 95, 88]
Local same = scores
same[0] = 70
Print(scores)
Local copy = scores.Clone()
copy[0] = 10
Print(scores, copy)
Print(scores == [70, 95, 88])[70, 95, 88] [70, 95, 88] [10, 95, 88] True
== compares two arrays’ values, one by one.
Out of range
Reading or writing an index that isn’t in the array stops the program with a panic, which says which index it was, how many values there are, and where.
Import std.core
Local scores = [72, 95, 88]
Print(scores[3])panic: index 3 is out of range for 3 elements at range.fb:4:7
at top-level code (range.fb:4)Fixed-size arrays
T[N] is an array of exactly N values, and T[W, H] a grid of them. Its size is part of its
type, so it can’t grow, and every value starts at its zero. Unlike a T[], it’s a value, copied
when it’s assigned, and stored in place, which suits small data of a known size.
Import std.core
Local seats: Bool[4, 2]
seats[1, 0] = True
seats[3, 1] = True
For row = 0 Until 2
Local line = ""
For column = 0 Until 4
line += seats[column, row] ? "X" : "."
End For
Print(line)
End For
Local copy = seats
copy[0, 0] = True
Print(seats[0, 0], copy[0, 0]).X.. ...X False True
What you learned
[a, b, c]makes aT[], whichAdd,Insert, andRemoveAtchange.- Indexes count from 0, and an index out of range panics.
- std.collections gives arrays
Sort,Filter,IndexOf, and many other Methods. - A
T[]is shared by reference;Clonecopies it, and==compares values. T[N]andT[W, H]are fixed-size arrays, copied like any value.
The rules are in the language reference.
Report an issue with this page or with FutureBlitz on GitHub.