Arrays contain a group of elements of the same data type, declared with a single variable name.
The array contains a fixed size of length.
Important points about Nim Arrays
- Arrays contain a collection of elements of the same data type
- Fixed-size
- Array index always starts with 0 and ends with length -1
- Index can also contain not only numbers but other ordinal types such as chars, strings
Let’s see different examples.
How to declare Arrays in Nim
Array declaration syntax
var variable: array[size, datatype]
Size is the number of elements in size. Datatype is a NIM predefined type. the array is a keyword variable is a valid identifier
Examples of declaring an array of different
An array of integers is declared with a fixed size and elements in the array are initialized with zero.
var numbers: array[10, int]
echo numbers
Output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
An array of floats are declared with a fixed size and elements in the array are initialized with default float zero.
var numbers: array[10, float]
echo numbers
Output:
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
An array of Strings is declared with size and elements in an array assigned with space.
var numbers: array[10, string]
echo numbers
Output:
["", "", "", "", "", "", "", "", "", ""]
An array of bool is declared with size and elements in an array assigned with a false value.
var numbers: array[10, bool]
echo numbers
Output:
[false, false, false, false, false, false, false, false, false, false]
Another way to declare an array with a dots operator
var ab: array[0..3, int]
echo ab
Output:
[0, 0, 0, 0]
How to access the array elements in a NIM array?
Array elements are accessed using an indexing mechanism.
The first element in the array starts with zero
and the last element is length -1
.
var numbers: array[5, int]
echo numbers[0] ## 0
echo numbers[4] ## 0
echo numbers[5] ## Error: index 5 not in 0 .. 4
Another way to get Start and End indexes using low
and high
procs
echo numbers.low ##0
echo numbers.high ##4
Length of an array
len
proc returns the number of elements in an array.
var numbers: array[15, int]
echo numbers.len
How to add elements into an array in NIm
Elements are added to an array using an index with square bracket syntax.
array[index]=value
For example, to add an array at the index position, use array[0]=1
Here is an example
var numbers: array[15, int]
echo numbers
numbers[0]=1
numbers[1]=2
numbers[2]=3
echo numbers
Nim Multi-dimensional array
Multi-dimensional arrays are arrays of sub-arrays.
Example multi-dimensional are two-dimensional arrays i.e matrix.
var matrix: array[2, array[2, int]]
echo matrix
matrix[0][0] = 10
matrix[0][1] = 11
matrix[1][0] = 12
matrix[1][1] = 13
echo matrix
Output:
[[0, 0], [0, 0]]
[[10, 11], [12, 13]]
Iterate an array with an example
Arrays elements iterated using for in loop syntax.
The below example iterates an array using value.
var ab: array[0..2, int]
for value in ab:
echo value
Output:
0
0
0
For loop contains an object that contains the index and value for each iteration.
var numbers: array[0..2, int]
for index, value in numbers:
echo index,"=", value
Output:
0=0
1=0
2=0