Array
Arrays in Cyclone
Syntax
Array Declaration
Array is declared using var / const keyword followed by the array name and a mandatory Type Clause ending with []:
var arr : int[] = {1, 2, 3, 4, 5}Ways of declaring an array
- Providing both declaration with size and initialization. Declared size and initialization size should be same.
var arr : int[5] = {1, 2, 3, 4, 5}- Providing only declaration. In this case, it is mandatory to provide the size of the array.
var arr : int[5]- Providing only declaration and initialization. In this case, the size of the array is automatically calculated.
var arr : int[] = {1, 2, 3, 4, 5}Accessing Array Elements
Array elements are accessed using the array name followed by the index enclosed in square brackets []:
var arr : int[] = {1, 2, 3, 4, 5}
vax x : int = arr[0]
// x = 1Assigning Array Elements
Array elements are assigned using the array name followed by the index enclosed in square brackets []:
var arr : int[] = {1, 2, 3, 4, 5}
arr[0] = 10
// {10, 2, 3, 4, 5}Array Length
Array length is accessed using the size in-built function:
var arr : int[5] = {1, 2, 3, 4, 5}
var len : int = size(arr)
// len = 5