Arrays

An array is a set of values that are logically related to each other. You refer to these values by the same variable name and use a number called an index or subscript to select each value. These values are known as the elements of the array and fully use a range from zero thru a specified maximum.

Variables other than arrays are known as scalars. A scalar can hold only one value at a time.

Creating an array

You make an array with the same statements you use to make other variables. The only difference is the dimensions of the array, which you put in parentheses. Some examples follow.

Dim my_first_array(10)

Because my_first_array uses one index or subscript, it is known as a onee-dimensional array. You also can make arrays with more than one subscript, usually known as multidimensional arrays. See Array Dimensions for more information.

Storing values in an array

You get access to each element in an array with an index of type Int. You write the name of the array, then the index between parentheses. Indexes for multidimensional arrays have a comma between each. You use one index for each array dimension.

Giving an array initial values

You can give an array some initial values with an array initializer. An array initializer is a list of values with a comma (,) between each, all between braces ({ }).

Dim days() = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }

Nested initializers

You can make arrays with many dimensions with nested array initializers.

Dim magic() = {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}

Iterating through an array

TODO2

Dim numbers() As Int = { 1, 2, 3 }

For index = 0 To numbers.Last
    PrintLine numbers(index)
End For
Output:
1
2
3
Dim numbers(*2) = {{1, 2}, {3, 4}, {5, 6}}

For row = 0 To numbers.Last(0)
    For column = 0 To numbers.Last(1)
        Print numbers(row, column) & $32
    End For
    PrintLine
End For
Output:
1 2
3 4
5 6
Dim numbers1D() As Int = { 1, 2, 3 }

For Each number In numbers1D
    PrintLine number
End For

Dim numbers2D(*2) As Int = {{4, 5}, {6, 7}, {8, 9}}

For Each number In numbers2D
   PrintLine number
End For
Output:
1
2
3
4
5
6
7
8
9

Arrays as return values and parameters

TODO2

Dim my_numbers() = GetNumbers()
ShowNumbers my_numbers

Sub GetNumbers() As Int*1
    Dim numbers() As Int = { 97, 114, 114, 97, 121 }
    Return numbers
End Sub

Sub ShowNumbers(numbers(*1) As Int)
    For Each number In numbers
        PrintLine number
    End For
End Sub
Output:
97
114
114
97
121

Array dimensions

TODO

See also