类型 函数 库 table.* 返回值 无 修订版 发行版 2024.3703 关键字 table、数组 另请参阅 table.remove()
将给定值插入表中。如果给定了位置,则在当前位于该位置的元素前插入该值。
table.insert( t, value ) table.insert( t, pos, value )
表. 将向其添加新值的一个表。当向表中插入元素时,数组的大小和元素索引都会更新。
数字. 将在该位置插入新元素的表的索引。默认值是表长度 + 1,以便 table.insert(t,x)
在表 t
的末尾插入 x
。
要分配到表中的新值。
当提供 2 个参数时,将忽略可选参数 pos
。
请注意,在向表中插入新值时,使用长度运算符更快捷:t[#t + 1] = x
。
local t = { 1, "jane" } table.insert( t, "doe" ) --> 1, jane, doe table.insert( t, 2 ) --> 1, jane, doe, 2 -- notice 'pos' parameter is ignored table.insert(t, 2, "miss") --> 1, miss, jane, doe, 2