Lua Arrays
An array is a collection of elements of the same data type arranged in a certain order, which can be a one-dimensional array or a multidimensional array.
In Lua, an array is not a specific data type, but a data structure used to store a group of values.
In fact, Lua does not have a dedicated array type; instead, it uses a data structure called"table"to implement the functionality of arrays.
The index key values of Lua arrays can be represented by integers, and the size of the array is not fixed.
In Lua, index values start at1as the starting point, but you can also specify starting from 0.
One-dimensional Array
A one-dimensional array is the simplest type of array, and its logical structure is a linear list.
Access array elements using indices:
Example
local myArray = {10, 20, 30, 40, 50}
-- Access array elements
print(myArray[1]) -- Output 10
print(myArray[3]) -- Output 30
The output of the above code execution is:
10 30
To calculate the length of an array (i.e., the number of elements in the array), you can use the#operator:
Example
-- Calculate the array length
local length = #myArray
print(length) -- Output 5
The output of the above code execution is:
5
A one-dimensional array can useforloop to iterate over the elements in the array, as in the following example:
Example
local myArray = {10, 20, 30, 40, 50}
-- Loop through the array
for i = 1, #myArray do
print(myArray[i])
end
The output of the above code execution is:
10 20 30 40 50
Lua indices start from 1 by default:
Example
for i= 0, 2 do
print(array[i])
end
The output of the above code execution is:
nil Lua Tutorial
As you can see, we can use integer indices to access array elements. If the specified index has no value, it returnsnil。
In addition, we can also use negative numbers as array index values:
Example
for i= -2, 2 do
array[i] = i *2
end
for i = -2,2 do
print(array[i])
end
The output of the above code execution is:
-4 -2 0 2 4
We can also modify elements in the array:
Example
local myArray = {10, 20, 30, 40, 50}
-- Modify array elements
myArray[2] = 25
-- Loop through the array
for i = 1, #myArray do
print(myArray[i])
end
The output of the above code execution is:
10 25 30 40 50
We can also add elements to the array:
Example
local myArray = {10, 20, 30, 40, 50}
-- Add a new element to the end of the array
myArray[#myArray + 1] = 60
-- Loop through the array
for i = 1, #myArray do
print(myArray[i])
end
The output of the above code execution is:
10 20 30 40 50 60
We can also delete elements from the array:
Example
local myArray = {10, 20, 30, 40, 50}
-- Delete the third element
table.remove(myArray, 3)
-- Loop through the array
for i = 1, #myArray do
print(myArray[i])
end
The output of the above code execution is:
10 20 40 50
Multidimensional Array
A multidimensional array is an array that contains arrays, or an array where the index key of a one-dimensional array corresponds to an array.
The following is a multidimensional array with three rows and three columns:
Example
array = {}
for i=1,3 do
array[i] = {}
for j=1,3 do
array[i][j] = i*j
end
end
-- Access the array
for i=1,3 do
for j=1,3 do
print(array[i][j])
end
end
The output of the above code execution is:
1 2 3 2 4 6 3 6 9
A multidimensional array with three rows and three columns using different index keys:
Example
array = {}
maxRows = 3
maxColumns = 3
for row=1,maxRows do
for col=1,maxColumns do
array[row*maxColumns +col] = row*col
end
end
-- Access the array
for row=1,maxRows do
for col=1,maxColumns do
print(array[row*maxColumns +col])
end
end
The output of the above code execution is:
1 2 3 2 4 6 3 6 9
As you can see, in the examples above, the array has specified index values set, which avoids nil values and helps save memory space.
Other Extensions