Arrays
An array can hold multiple values of a specific type. The memory must be preallocated.
The structure of a variable of array type:
Simple Example:
A value of the array can be accessed though the index operator in square brackets. Every item has an index to indentify an element. An index starts at zero, so the first element is accessable at index 0, the second element is accessible at index 1 and so on. The index has to be within the bounds of the array. It cannot be negative or greater than or equal to the length of the array.
An item in the array can be also changed, even if the variable that contains it is not mutable.
The example above is an array of 1 dimension. You can also define arrays of more dimensions to build a matrix.
To declare a n-dimensional array:
Exercises
Declare an array containing the first ten prime numbers and print it to the console.
Last updated