表是Lua中唯一可用的數據結構,使用表可以創建不同的類型,如數組和字典。 Lua使用關聯數組,不僅可使用數字編制索引,還可以使用除nil
之外的字串編制索引。 表沒有固定的大小,可以根據需要增長大小。
Lua在所有表示中使用表,包括包的表示。 當訪問方法string.format
時,這意味著,訪問字串包中可用的格式函數。
表示和用法
表稱為對象,它既不是值也不是變數。 Lua使用構造函數運算式{}
來創建一個空表。 應該知道,保持表的引用的變數和表本身之間沒有固定的關係。
--sample table initialization
mytable = {}
--simple table value assignment
mytable[1]= "Lua"
--removing reference
mytable = nil
-- lua garbage collection will take care of releasing memory
假設有一個包含元素集的表a
,如果將它分配給表b
,則a
和b
都指向相同的記憶體。 Lua不會單獨為b
分配單獨的記憶體。 當a
設置為nil
時,b
仍然可以訪問表。 當沒有對表的引用時,Lua中的垃圾收集器負責清理進程以使這些未引用的記憶體再次被重用。
下麵示出了一個例子,用於解釋表的上述特徵。
-- Simple empty table
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 and mytable refers to same table
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"])
-- only variable released and and not table
alternatetable = nil
print("alternatetable is ", alternatetable)
-- mytable is still accessible
print("mytable Element at index wow is ", mytable["wow"])
mytable = nil
print("mytable is ", mytable)
當運行上述程式時,將獲得以下輸出 -
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
表操作
下麵是用於表操作的內置函數,它們列在下表格中。
編號 | 方法 | 作用 |
---|---|---|
1 | table.concat (table [, sep [, i [, j]]]) |
根據給定的參數連接表中的字串。詳細資訊請參見示例。 |
2 | table.insert (table, [pos,] value) |
在指定位置的表中插入值。 |
3 | table.maxn (table) |
返回最大的數字索引。 |
4 | table.remove (table [, pos]) |
從表中刪除值。 |
5 | table.sort (table [, comp]) |
根據可選的比較器參數對表進行排序。 |
下麵來看看一些上述功能的示例。
1. 表連接
使用concat
函數來連接兩個表,如下所示 -
fruits = {"banana","orange","apple"}
-- returns concatenated string of table
print("Concatenated string ",table.concat(fruits))
--concatenate with a character
print("Concatenated string ",table.concat(fruits,", "))
--concatenate fruits based on index
print("Concatenated string ",table.concat(fruits,", ", 2,3))
當運行上述程式時,將獲得以下輸出 -
Concatenated string bananaorangeapple
Concatenated string banana, orange, apple
Concatenated string orange, apple
2. 插入和刪除
在表操作中,最常見的是在表中插入和刪除專案。如下解釋。
fruits = {"banana","orange","apple"}
-- insert a fruit at the end
table.insert(fruits,"mango")
print("Fruit at index 4 is ",fruits[4])
--insert fruit at index 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])
當運行上述程式時,將獲得以下輸出 -
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
3. 排序表
有時想要按特定順序對表進行排序。 排序函數按字母順序對表中的元素進行排序。 這方面的示例如下所示。
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
當運行上述程式時,將獲得以下輸出 -
1 banana
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange