# Array type
The array type in glua is implemented with a dynamic array and has a variable length
The array type is a data structure that can store several values of the same type. The type declaration syntax is Array
, where T is replaced by a specific type, The array literal syntax is
[ value1, value2, … ]
E.g:
let a: Array<int> = [ 1, 2, 3 ] -- Note that the two symbols ``>'' and ``='' cannot be connected together
let b: int = a[1]
- Array types and array literals support nested and protected table literals
let b: int = a[1]
E.g:
type Person = { name: string, age: int default 0 }
let p1 = Person({name='p1'})
let p2 = Person({name='p2', age=24})
let persons: Array<Array<Person> > = [ [p1, p2], [p2, p3], [p3] ] -- Note that here ``>'' and ``>'' cannot be connected together, otherwise it will be recognized as ``>>''
Array<T>
The addition, deletion and modification of the value of the type can be operated using the built-in module table module
E.g:
var a = [1,2,3]
table.append(a, 4) -- Add value to array
a[3] = 100 -- Modify the third element of the array
let array_length1 = table.length(a) -- Get array length
let array_length2 = #a -- Another way to get the length of an array
table.remove(a, 2) -- Delete the second element in the a array (1-based index)
var k: int, v: int = 0, 0
for k, v in pairs(a) do
pprint(k, v) -- Traverse the array a, k and v are the index and value of each item in a, the index starts from 1.
end