table 在 Lua 里是一種重要的數據結構,它可以說是其他數據結構的基礎,通常的數組、記錄、線性表、隊列、集合等數據結構都可以用 table 來表示,甚至連全局變量(_G)、模塊、元表(metatable)等這些重要的 Lua 元素都是 table 的結構。可以說,table 是一個強大而又神奇的東西。
table 特性
在之前介紹 Lua 數據類型時,也說過了 table 的一些特性,簡單列舉如下(詳情可查看之前的介紹):
1.table是一個“關聯數組”,數組的索引可以是數字或者是字符串
2.table 的默認初始索引一般以 1 開始
3.table 的變量只是一個地址引用,對 table 的操作不會產生數據影響
4.table 不會固定長度大小,有新數據插入時長度會自動增長
5.table 的方法函數
Lua 5.2.2 內置有以下 7 中對 table 操作的方法:
concat
函數 table.concat 主要用來把表里的每個元素通過一個分隔符(separator)連接組合起來,用法:
復制代碼 代碼如下:
table.concat(table, sep, start, end)
上面除 table 外, sep、start、end 這三個參數都是可選的,并且順序讀入的,如果沒指定傳入,函數 concat 會采用默認值(分隔符 sep 的默認值是空字符, start 的默認值是 1, end 的默認值是數組部分的總長)去執行。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
print(table.concat(tbl))
print(table.concat(tbl, "、"))
print(table.concat(tbl, "、", 2))
print(table.concat(tbl, "、", 2, 3))
對于密集型的字符并接,table.concat 比用 ".." 連接更高效,下面是用 time 測試的效果:
復制代碼 代碼如下:
local str = {}
for i = 1, 10000 do
str[i] = "str"
end
print(table.concat(str))
--real 0m0.005s
--user 0m0.003s
--sys 0m0.002s
復制代碼 代碼如下:
local str = ""
for i = 1, 10000 do
str = str .. "str"
end
print(str)
--real 0m0.041s
--user 0m0.037s
--sys 0m0.002s
insert
函數 table.insert 用于向 table 的指定位置(pos)插入一個新元素,用法:
復制代碼 代碼如下:
table.insert(table, pos value)
參數 pos 是可選,默認是 table 末尾位置。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
table.insert(tbl, "watermelon")
print(table.concat(tbl, "、"))
table.insert(tbl, 2, "watermelon")
print(table.concat(tbl, "、"))
maxn
函數 table.maxn 是返回 table 最大的正數索引值,用法:
復制代碼 代碼如下:
table.maxn (table)
如果不存在正數的索引值,則返回 0。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
print(table.maxn(tbl))
local tbl = {"apple", "pear", "orange", "grape", [26] = "watermelon"}
print(table.maxn(tbl))
local tbl = {[-1] = "apple", [-2] = "pear", [-3] = "orange", [-4] = "grape"}
print(table.maxn(tbl))
pack
函數 table.pack 是獲取一個索引從 1 開始的參數表 table,并會對這個 table 預定義一個字段 n,表示該表的長度,用法:
復制代碼 代碼如下:
table.pack(···)
該函數常用在獲取傳入函數的參數。
復制代碼 代碼如下:
function table_pack(param, ...)
local arg = table.pack(...)
print("this arg table length is", arg.n)
for i = 1, arg.n do
print(i, arg[i])
end
end
table_pack("test", "param1", "param2", "param3")
remove
函數 table.remove 用于刪除 table 里某個值,用法:
復制代碼 代碼如下:
table.remove (list [, pos])
參數 pos 可選,默認為刪除 table 最后一個元素,并且參數 pos 的類型只能是數字 number 類型。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
table.remove(tbl, 2)
print(table.concat(tbl, "、"))
table.remove(tbl)
print(table.concat(tbl, "、"))
sort
函數 table.sort 用于對 table 里的元素作排序操作,用法:
復制代碼 代碼如下:
table.sort(table, comp)
參數 comp 是一個排序對比函數,它有兩個參數 param1、param2,如果 param1 排在 param2 前面,那么排序函數返回 true,否則返回 false。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
local sort_func1 = function(a, b) return a > b end
table.sort(tbl, sort_func1)
print(table.concat(tbl, "、"))
local sort_func2 = function(a, b) return a b end
table.sort(tbl, sort_func2)
print(table.concat(tbl, "、"))
參數 comp 可選,缺省 comp 的情況下是對表作升序排序。
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
table.sort(tbl)
print(table.concat(tbl, "、"))
unpack
函數 table.unpack 用于返回 table 里的元素,用法:
復制代碼 代碼如下:
table.unpack(table, start, end)
參數 start 是開始返回的元素位置,默認是 1,參數 end 是返回最后一個元素的位置,默認是 table 最后一個元素的位置,參數 start、end 都是可選
復制代碼 代碼如下:
local tbl = {"apple", "pear", "orange", "grape"}
print(table.unpack(tbl))
local a, b, c, d = table.unpack(tbl)
print(a, b, c, d)
print(table.unpack(tbl, 2))
print(table.unpack(tbl, 2, 3))
您可能感興趣的文章:- Lua的table庫函數insert、remove、concat、sort詳細介紹
- Lua中table的幾種構造方式詳解
- Lua中對table排序實例
- Lua中遍歷數組和table的4種方法
- Lua中使用table.concat連接大量字符串實例
- Lua中的table淺析
- Lua判斷Table是否為空的方法(空的table即{})
- Lua中使用table實現的其它5種數據結構
- 獲取Lua表結構(table)數據實例
- 深入談談lua中神奇的table