What's the difference between table.insert(t, i) and t[#t+1] = i?

前端 未结 4 2047
旧巷少年郎
旧巷少年郎 2021-02-05 02:39

In Lua, there seem to be two ways of appending an element to an array:

table.insert(t, i)

and

t[#t+1] = i

Whi

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-05 03:06

    insert can insert arbitrarily (as its name states), it only defaults to #t + 1, where as t[#t + 1] = i will always append to the (end of the) table. see section 5.5 in the lua manual.

提交回复
热议问题