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

前端 未结 4 2039
旧巷少年郎
旧巷少年郎 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:23

    '#' operator only use indexed key table.

    t = {1, 2 ,3 ,4, 5, x=1, y=2}
    

    at above code

    print(#t)  --> print 5 not 7
    

    '#' operator whenever not using.

    If you want to '#' operator, then check it to table elements type.

    Insert function can using any type use.But element count to work slow than '#'

提交回复
热议问题