数组初始化

数组和集合 / 数组

-- 空数组
local n1 = {}
local n1_length = #n1
-- n1_length = 0

-- 一维数组,从 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

-- 多维数组:各行长度可以不同
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)