1.结论:lua5.4版本的#运算符,其结果为:表的一个随机边界index.
2.边界index:值为nil的元素的pre元素的index(0 <= 边界index <= math.maxinteger)
例_1:{1, 2, 3} --> [4] = nil,边界index为3
例_2:{1, 2, nil, 4, 5} --> [3] = nil,[6] = nil边界index为2和5
例_3:{nil, 1, 2} --> [1] = nil, [4] = nil,边界index为1或3
例_4:{2, 3, nil, 3, nil, 4} --> [3] = nil, [5] = nil, [7] = nil, 边界index为2, 4, 6
例_5:{[1] = 1, [2] = 2, [4] = 4, [5] = 5, [6] = 6, [7] = 7} --> [3] = nil,[8] = nil,边界index为2和7
例_6:{[1] = 1, [2] = 2, [3] = 4, [5] = 5, [6] = 6, [7] = 7} --> [4] = nil,[8] = nil,边界index为3和7
3.#table的时候会根据table的address取具体的边界index,所以上述例子输出结果:
- > t = {1, 2, 3}
- > #t
- 3
- > t = {1, 2, nil, 4, 5}
- > #t
- 5
- > t = {nil, 1, 2}
- > #t
- 3
- > t = {2, 3, nil, 3, nil, 4}
- > #t
- 6
- > t = {[1] = 1, [2] = 2, [4] = 4, [5] = 5, [6] = 6, [7] = 7}
- > #t
- 7
- > t = {[1] = 1, [2] = 2, [3] = 4, [5] = 5, [6] = 6, [7] = 7}
- > #t
- 3
复制代码
|