数组初始化

数组和集合 / 数组

-- Empty array
local n1 = {}
local n1_length = #n1
-- n1_length = 0

-- Single-dimensional arrays, counted from 1
local n2 = {1, 2, 3}
local first = n2[1]
-- first = 1
local n3 = {"1", "2", "3"}
local texts = table.concat(n3, ", ")
-- texts = 1, 2, 3

-- Multidimensional: rows may differ in length
local n4 = {{1, 2}, {3, 4, 5}}
local row1_length = #n4[1]
-- row1_length = 2
local row2_length = #n4[2]
-- row2_length = 3
local last = n4[2][3]
-- last = 5

print("n1_length = " .. n1_length)
print("first = " .. first)
print("texts = " .. texts)
print("row1_length = " .. row1_length)
print("row2_length = " .. row2_length)
print("last = " .. last)