为什么Lua的长度(#)运算符返回意外值?
Lua具有#运算符来计算用作数组的表的“length”.
我检查了这个运算符,我很惊讶. 这是我在Lua 5.2.3下运行的代码: t = {}; t[0] = 1; t[1] = 2; print(#t); -- 1 aha lua counts from one t[2] = 3; print(#t); -- 2 tree values,but only two are count t[4] = 3; print(#t); -- 4 but 3 is mssing? t[400] = 400; t[401] = 401; print(#t); -- still 4,now I am confused? t2 = {10,20,nil,40} print(#t2); -- 4 but documentations says this is not a sequence? 有人可以解释规则吗? 解决方法
引用Lua 5.2参考手册:
the length of a table t is only defined if the table is a sequence,that is,the set of its positive numeric keys is equal to {1..n} for some integer n 非序列上的#运算符的结果未定义. 背景:Lua中的表格内部分为数组部分和散列部分.这是一个优化. Lua试图避免经常分配内存,所以它预先分配两个下一个权力.这是另一个优化. >当数组部分的最后一个项为nil时,#的结果是通过bin搜索第一个nil-follow键的数组部分找到的最短有效序列的长度. 所以#的结果几乎总是最短有效序列的(期望的)长度,除非表示非序列的数组部分中的最后一个元素是非零的.那么结果比想要的要大. 这是为什么?这似乎是另一个优化(对于两个大小的数组).这种表上的#的复杂度是O(1),而其他变体是O(log(n))). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |