为什么在表中,

问题描述 投票:0回答:1
最终以索引2结束,在表中,它最终到达索引

10

x
	
5
,我们可以看到
local t = {1, nil, 2} table.insert(t,10) for i = 1, 6 do print(string.format("t[%d] = %s", i, t[i])) end print() local y = {1, nil, 2} y[5]= 3 table.insert(y, 10) for i = 1, 6 do print(string.format("y[%d] = %s", i, y[i])) end print() local x = {1, nil, 2} table.insert(x,2,20) x[6]= 3 table.insert(x,10) for i = 1, 6 do print(string.format("x[%d] = %s", i, x[i])) end Terminal result: t[1] = 1 t[2] = nil t[3] = 2 t[4] = 10 t[5] = nil t[6] = nil y[1] = 1 y[2] = 10 y[3] = 2 y[4] = nil y[5] = 3 y[6] = nil x[1] = 1 x[2] = 20 x[3] = nil x[4] = 2 x[5] = 10 x[6] = 3
行为
table.insert

lua lua-table
1个回答
0
投票

在表上应用的长度运算符返回该表中的边框。表T中的边框是满足以下条件的任何非负整数:
table.insert(t, 42)
[...]

一个完全一个
border的表称为

。 [...] 当t是一个序列时,#t返回其唯一的边框,这对应于序列长度的直观概念。 当t不是一个序列时,#t可以返回其任何边界。非数字密钥。)

其他单词,在具有多个边界的表上调用
t[#t + 1] = 42

未指定位置的表,将在表格的内部表示确定的索引上插入值。 如果可移植性是一个问题,请避免这种情况。例如,在我的系统上,您的程序的结果在Lua和Luajit的参考实现之间有所不同。

(border == 0 or t[border] ~= nil) and (t[border + 1] == nil or border == math.maxinteger)
    


最新问题
© www.soinside.com 2019 - 2024. All rights reserved.