在 Lua 語言中,表是唯一可以用來創(chuàng)建不同數(shù)據(jù)類型的數(shù)據(jù)結構,比如常見的數(shù)組和字典都是用表來創(chuàng)建的。 Lua 語言中經(jīng)常到關聯(lián)數(shù)組這種數(shù)據(jù)類型,它不僅可以用數(shù)值作為索引值,除了 nil 以外的字符串同樣可以作為其索引。表沒有固定的大小,當數(shù)據(jù)量增加時表會自動增大。
Lua 語言中的各種結構表示都用到了表,包括包(package)的表示。當我們使用方法 string.format 時,我們用到的其實是包 string 中的方法 format。
表被稱之為對象,它既不是值也不是變量。Lua 用構造表達式 {} 創(chuàng)建一個空表。需要注意的是,在存儲表的變量和表本身之間沒有什么固定的對應關系(譯注:一個表可以被不同的變量引用,一個變量也可以隨時改變其所引用的表對象)。
--簡單的表初始化
mytable = {}
--簡單的表賦值
mytable[1]= "Lua"
--移除引用
mytable = nil
-- lua 的垃圾回收機制負責回收內存空間
當我們有一個擁有一系列元素的表時,如果我們將其賦值給 b。那么 a 和 b 都會引用同一個表對象(a 先引用該表),指向相同的內存空間。而不會再單獨為 b 分配內存空間。即使給變量 a 賦值 nil,我們仍然可以用變量 b 訪問表本身。如果已經(jīng)沒有變量引用表時,Lua 語言垃圾回收機制負責回收不再使用的內存以被重復使用。
下面的示例代碼使用到了上面提到的表的特性?! ?/p>
-- 聲明空表
mytable = {}
print("Type of mytable is ",type(mytable))
mytable[1]= "Lua"
mytable["wow"] = "Tutorial"
print("mytable Element at index 1 is ", mytable[1])
print("mytable Element at index wow is ", mytable["wow"])
-- alternatetable 與 mytable 引用相同的表
alternatetable = mytable
print("alternatetable Element at index 1 is ", alternatetable[1])
print("mytable Element at index wow is ", alternatetable["wow"])
alternatetable["wow"] = "I changed it"
print("mytable Element at index wow is ", mytable["wow"])
-- 只是變量被釋放,表本身沒有被釋放
alternatetable = nil
print("alternatetable is ", alternatetable)
-- mytable 仍然可以訪問
print("mytable Element at index wow is ", mytable["wow"])
mytable = nil
print("mytable is ", mytable)
執(zhí)行上面的代碼,我們可以得到如下的輸出結果:
Type of mytable is table
mytable Element at index 1 is Lua
mytable Element at index wow is Tutorial
alternatetable Element at index 1 is Lua
mytable Element at index wow is Tutorial
mytable Element at index wow is I changed it
alternatetable is nil
mytable Element at index wow is I changed it
mytable is nil
下面的表中列出了 Lua 語言內置的表操作函數(shù),具體內容如下所示:
S.N. | 方法與作用 |
---|---|
1 | table.concat(table[, sep [, i[,j]]]): 根據(jù)指定的參數(shù)合并表中的字符串。具體用法參考下面的示例。 |
2 | table.insert(table,[pos,]value):在表中指定位置插入一個值。 |
3 | table.maxn(table):返回表中最大的數(shù)值索引。 |
4 | table.remove(table[,pos]):從表中移出指定的值。 |
5 | table.sort(table[,comp]):根據(jù)指定的(可選)比較方法對表進行排序操作。 |
讓我們一起看一些上述函數(shù)使用的例子。
我們可以使用表連接操作連接表中的元素,如下所示。
fruits = {"banana","orange","apple"}
-- 返回表中字符串連接后的結果
print("Concatenated string ",table.concat(fruits))
--用字符串連接
print("Concatenated string ",table.concat(fruits,", "))
--基于索引連接 fruits
print("Concatenated string ",table.concat(fruits,", ", 2,3))
執(zhí)行上面的代碼,我們可以得到如下的輸出結果:
Concatenated string bananaorangeapple
Concatenated string banana, orange, apple
Concatenated string orange, apple
插入和移除表中元素是對表最常見的操作。使用方法如下所示:
fruits = {"banana","orange","apple"}
-- 在 fruits 的末尾插入一種水果
table.insert(fruits,"mango")
print("Fruit at index 4 is ",fruits[4])
-- 在索引 2 的位置插入一種水果
table.insert(fruits,2,"grapes")
print("Fruit at index 2 is ",fruits[2])
print("The maximum elements in table is",table.maxn(fruits))
print("The last element is",fruits[5])
table.remove(fruits)
print("The previous last element is",fruits[5])
執(zhí)行上面的代碼,我們可以得到如下的輸出結果:
Fruit at index 4 is mango
Fruit at index 2 is grapes
The maximum elements in table is 5
The last element is mango
The previous last element is nil
在程序開發(fā)過程中,常常有對表排序的需求。 sort 函數(shù)默認使用字母表對表中的元素進行排序(可以通過提供比較函數(shù)改變排序策略)。示例代碼如下:
fruits = {"banana","orange","apple","grapes"}
for k,v in ipairs(fruits) do
print(k,v)
end
table.sort(fruits)
print("sorted table")
for k,v in ipairs(fruits) do
print(k,v)
end
執(zhí)行上面的代碼,我們可以得到如下的輸出結果:
1 banana
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange