table.getn(tableName)得到一个table的大小,等同于操作符#
需要注意的是:该table的key必须是有序的,索引是从1开始
–例如有序的
local xiang = {10,22,34,42,51}
print(“xiang length ==”,#(xiang)) –结果为:[LUA-print] xiang length == 5
–例如无序的
local song = {s=10,h=22,x=34,m=42,n=51}
print(“song length ==”,#(song)) –结果为:[LUA-print] song length == 0
–对于无序的我们可以这样做
local count = 0
for k,v in pairs(song) do
count = count + 1
end
print(“song length ==”,count) –结果为: [LUA-print] song length == 5
注:在MAC OS Lua 5.2.2中table.getn()无法使用,用#代替