lua获取table 的长度

版权声明:欢迎学习交流 https://blog.csdn.net/weixin_42493768/article/details/87856437

当我们获取 table 的长度的时候无论是使用 # 还是 table.getn 其都会在索引中断的地方停止计数,而导致无法正确取得 table 的长度。

可以使用以下方法来代替:

function table_leng(t)
local leng=0
for k, v in pairs(t) do
leng=leng+1
end
return leng;
end

猜你喜欢

转载自blog.csdn.net/weixin_42493768/article/details/87856437