It is possible to declare array variables by appending the [] brackets to the type. When declaring a variable with a type modifier, i.e. [], the type modifier affects the type of all variables in the list. Example:
int[] a, b, c;
a
, b
, and c
are now arrays of integers.
When declaring arrays it is possible to define the initial size of the array by passing the length as a parameter to the constructor. The elements can also be individually initialized by specifying an initialization list. Example:
int[] a; // A zero-length array of integers int[] b(3); // An array of integers with 3 elements int[] c(3, 1); // An array of integers with 3 elements, all set to 1 by default int[] d = {,3,4,}; // An array of integers with 4 elements, where // the second and third elements are initialized
Multidimensional arrays are supported as arrays of arrays, for example:
int[][] a; // An empty array of arrays of integers int[][] b = {{1,2},{3,4}} // A 2 by 2 array with initialized values int[][] c(10, int[](10)); // A 10 by 10 array of integers with uninitialized values
Each element in the array is accessed with the indexing operator. The indices are zero based, i.e the range of valid indices are from 0 to length - 1.
a[0] = some_value;
The standard array implementation also has the following methods:
// Adding and removing elements void insertAt(uint index, const T& in); void removeAt(uint index); void insertLast(const T& in); void removeLast();
// Determine size of array uint length() const; void resize(uint);
// Sort the array void sortAsc(); void sortAsc(uint index, uint count); void sortDesc(); void sortDesc(uint index, uint count); void reverse();
// Find elements int find(const T& in); int find(uint index, const T& in);