2017-09-26 94 views
0
a = {} 
a[1] = 1 
a[2] = nil -- does nothing, as a[2] is already nil 
a[3] = 14 

print(#a) 

打印1的Lua - 表的長度似乎無效

a = {} 
a[1] = 1 
a[2] = nil -- does nothing, as a[2] is already nil 
a[3] = 14 
a[4] = 5 

print(#a) 

打印4

什麼是#運營商真正迴歸?

回答

2

如果表格不正確,則應用於該表格的#運算符的返回值未定義。

在這兩種情況下,a都不是正確的順序,因爲a[2] == nila[3] ~= nil

#運算符返回正確序列的長度。