1.字串作為table索引
法1.mytable =
法2.mytable =
法3.mytable = {}
mytable.x = "xixi"
法4.mytable = {}
mytable["x"] = "xixi"
注:當使用數字字串作為索引的時候(如:「10」):
法1(錯誤):mytable =
法2:mytable =
法3(錯誤):mytable = {} mytable.10 = "xixi"
法4:mytable = {} mytable["10"] = "xixi"
2.數字作為table索引
法1.mytable =
法2.mytable = {}
mytable[2] = "xixi"
法3.mytable =
這時候預設起始座標為1,所以值為"y"對應的是mytable[2]
3.字串作為table索引,值為函式
法1.x =
法2.y =
法3.z = {}
z.func =
function ()
print("z func call")
end法4.
w = {}
w["func"] =
function ()
print("w func call")
end法5.(新增定義方法)
m = {}
function m.func()
print("m func call")
end法1.
mytable[index]
法2.mytable.index
polyline = ,
print(polyline.color) --blue
print(polyline[1]) --test
print(polyline[2].x) --0
lua之table的使用
本文對從資料結構,儲存和使用三個角度對lua之table的使用進行小結。資料結構 table是lua的一種資料結構,可以用來存放各種型別的元素,例如可以建立陣列和字典,以及二者混合的資料結構,例如 a 陣列 b 字典 c 陣列元素和字典元素混合 儲存 lua的table使用陣列和雜湊表結合的方式,使...
Lua學習筆記之table
零散知識點記錄 table的拷貝是淺拷貝,若要深拷貝a b則需要遍歷b中的元素,並將其一一對應賦值給a。乙個table剛初始化時,是沒有metatable的,必須通過setmetatable給table賦予metatable,table才有metatable。所以若想直接用table 來初始化乙個t...
Lua資料之table函式
table.getn 獲取長度 取長度操作符寫作一元操作 字串的長度是它的位元組數 就是以乙個字元乙個位元組計算的字串長度 對於常規的陣列,裡面從 1 到 n 放著一些非空的值的時候,它的長度就精確的為 n,即最後乙個值的下標。如果陣列有乙個 空洞 就是說,nil 值被夾在非空值之間 那麼 t 可能...