YouTip LogoYouTip

Lua Arrays

An array is a collection of elements of the same data type arranged in a certain order. It can be a one-dimensional array or a multi-dimensional array. In Lua, an array is not a specific data type, but a data structure used to store a set of values. In fact, Lua does not have a dedicated array type. Instead, it uses a data structure called a **"table"** to implement array functionality. The index keys of a Lua array can be represented by integers, and the size of the array is not fixed. In Lua, index values start from 1, but you can also specify a start from 0. * * * ## One-Dimensional Arrays A one-dimensional array is the simplest type of array, with a logical structure of a linear list. Access array elements using indices: ## Example -- Create an array local myArray = {10, 20, 30, 40, 50} -- Access array elements print(myArray) -- Output: 10 print(myArray) -- Output: 30 The output of the above code 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 local myArray = {10, 20, 30, 40, 50} -- Calculate array length local length = #myArray print(length) -- Output: 5 The output of the above code is: 5 A one-dimensional array can be traversed using a **for** loop, as shown in the following example: ## Example -- Create an array local myArray = {10, 20, 30, 40, 50} -- Loop through the array for i = 1, #myArray do print(myArray) end The output of the above code is: 10 20 30 40 50 Lua indices default to starting from 1: ## Example array = {"Lua", "Tutorial"} for i = 0, 2 do print(array) end The output of the above code 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 returns **nil**. In addition, we can also use negative numbers as array index values: ## Example array = {} for i = -2, 2 do array = i * 2 end for i = -2, 2 do print(array) end The output of the above code is: -4 -2 0 2 4 We can also modify elements in an array: ## Example -- Create an array local myArray = {10, 20, 30, 40, 50} -- Modify an array element myArray = 25 -- Loop through the array for i = 1, #myArray do print(myArray) end The output of the above code is: 10 25 30 40 50 We can also add elements to an array: ## Example -- Create an array 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) end The output of the above code is: 10 20 30 40 50 60 We can also remove elements from an array: ## Example -- Create an array local myArray = {10, 20, 30, 40, 50} -- Remove the third element table.remove(myArray, 3) -- Loop through the array for i = 1, #myArray do print(myArray) end The output of the above code is: 10 20 40 50 * * * ## Multi-Dimensional Arrays A multi-dimensional array is an array that contains arrays, or a one-dimensional array whose index key corresponds to another array. The following is a three-by-three matrix multi-dimensional array: ## Example -- Initialize the array array = {} for i = 1, 3 do array = {} for j = 1, 3 do array = i * j end end -- Access the array for i = 1, 3 do for j = 1, 3 do print(array) end end The output of the above code is: 1 2 3 2 4 6 3 6 9 A three-by-three matrix multi-dimensional array with different index keys: ## Example -- Initialize the array 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 is: 1 2 3 2 4 6 3 6 9 As you can see, in the examples above, the array has specified index values. This helps avoid nil values and is beneficial for saving memory space.
← Lua EnvironmentLua Miscellaneous Operator β†’