佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > Lua中的table學(xué)習(xí)筆記

Lua中的table學(xué)習(xí)筆記

熱門標簽:鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 Linux服務(wù)器 服務(wù)外包 地方門戶網(wǎng)站 AI電銷 百度競價排名 呼叫中心市場需求

table 在 Lua 里是一種重要的數(shù)據(jù)結(jié)構(gòu),它可以說是其他數(shù)據(jù)結(jié)構(gòu)的基礎(chǔ),通常的數(shù)組、記錄、線性表、隊列、集合等數(shù)據(jù)結(jié)構(gòu)都可以用 table 來表示,甚至連全局變量(_G)、模塊、元表(metatable)等這些重要的 Lua 元素都是 table 的結(jié)構(gòu)。可以說,table  是一個強大而又神奇的東西。

table 特性

在之前介紹 Lua 數(shù)據(jù)類型時,也說過了 table 的一些特性,簡單列舉如下(詳情可查看之前的介紹):

1.table是一個“關(guān)聯(lián)數(shù)組”,數(shù)組的索引可以是數(shù)字或者是字符串
2.table 的默認初始索引一般以 1 開始
3.table 的變量只是一個地址引用,對 table 的操作不會產(chǎn)生數(shù)據(jù)影響
4.table 不會固定長度大小,有新數(shù)據(jù)插入時長度會自動增長
5.table 的方法函數(shù)

Lua 5.2.2 內(nèi)置有以下 7 中對 table 操作的方法:

concat

函數(shù) table.concat 主要用來把表里的每個元素通過一個分隔符(separator)連接組合起來,用法:

復(fù)制代碼 代碼如下:

table.concat(table, sep,  start, end)

上面除 table 外, sep、start、end 這三個參數(shù)都是可選的,并且順序讀入的,如果沒指定傳入,函數(shù) concat 會采用默認值(分隔符 sep 的默認值是空字符, start 的默認值是 1, end 的默認值是數(shù)組部分的總長)去執(zhí)行。

復(fù)制代碼 代碼如下:

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 測試的效果:

復(fù)制代碼 代碼如下:

local str = {}
for i = 1, 10000 do
    str[i] = "str"
end
print(table.concat(str))
--real  0m0.005s
--user  0m0.003s
--sys   0m0.002s

復(fù)制代碼 代碼如下:

local str = ""
for i = 1, 10000 do
    str = str .. "str"
end
print(str)
--real  0m0.041s
--user  0m0.037s
--sys   0m0.002s

insert

函數(shù) table.insert 用于向 table 的指定位置(pos)插入一個新元素,用法:

復(fù)制代碼 代碼如下:

table.insert(table, pos value)

參數(shù) pos 是可選,默認是 table 末尾位置。

復(fù)制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
 
table.insert(tbl, "watermelon")
print(table.concat(tbl, "、"))
 
table.insert(tbl, 2, "watermelon")
print(table.concat(tbl, "、"))

maxn

函數(shù) table.maxn 是返回 table 最大的正數(shù)索引值,用法:

復(fù)制代碼 代碼如下:

table.maxn (table)

如果不存在正數(shù)的索引值,則返回 0。

復(fù)制代碼 代碼如下:

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

函數(shù) table.pack 是獲取一個索引從 1 開始的參數(shù)表 table,并會對這個 table 預(yù)定義一個字段 n,表示該表的長度,用法:

復(fù)制代碼 代碼如下:

table.pack(···)

該函數(shù)常用在獲取傳入函數(shù)的參數(shù)。

復(fù)制代碼 代碼如下:

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

函數(shù) table.remove 用于刪除 table 里某個值,用法:

復(fù)制代碼 代碼如下:

table.remove (list [, pos])

參數(shù) pos 可選,默認為刪除 table 最后一個元素,并且參數(shù) pos 的類型只能是數(shù)字 number 類型。

復(fù)制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
table.remove(tbl, 2)
print(table.concat(tbl, "、"))
 
table.remove(tbl)
print(table.concat(tbl, "、"))

sort

函數(shù) table.sort 用于對 table 里的元素作排序操作,用法:

復(fù)制代碼 代碼如下:

table.sort(table, comp)

參數(shù) comp 是一個排序?qū)Ρ群瘮?shù),它有兩個參數(shù) param1、param2,如果 param1 排在 param2 前面,那么排序函數(shù)返回 true,否則返回 false。

復(fù)制代碼 代碼如下:

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, "、"))

參數(shù) comp 可選,缺省 comp 的情況下是對表作升序排序。

復(fù)制代碼 代碼如下:

local tbl = {"apple", "pear", "orange", "grape"}
table.sort(tbl)
print(table.concat(tbl, "、"))

unpack

函數(shù) table.unpack 用于返回 table 里的元素,用法:

復(fù)制代碼 代碼如下:

table.unpack(table, start, end)

參數(shù) start 是開始返回的元素位置,默認是 1,參數(shù) end 是返回最后一個元素的位置,默認是 table 最后一個元素的位置,參數(shù) start、end 都是可選

復(fù)制代碼 代碼如下:

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庫函數(shù)insert、remove、concat、sort詳細介紹
  • Lua中table的幾種構(gòu)造方式詳解
  • Lua中對table排序?qū)嵗?/li>
  • Lua中遍歷數(shù)組和table的4種方法
  • Lua中使用table.concat連接大量字符串實例
  • Lua中的table淺析
  • Lua判斷Table是否為空的方法(空的table即{})
  • Lua中使用table實現(xiàn)的其它5種數(shù)據(jù)結(jié)構(gòu)
  • 獲取Lua表結(jié)構(gòu)(table)數(shù)據(jù)實例
  • 深入談?wù)刲ua中神奇的table

標簽:衡水 仙桃 黃山 湖南 蘭州 湘潭 崇左 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Lua中的table學(xué)習(xí)筆記》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    屯留县| 盐山县| 岳池县| 漳浦县| 凤阳县| 娱乐| 天水市| 阿尔山市| 华坪县| 珲春市| 宣武区| 磐安县| 双桥区| 广河县| 桐庐县| 昭苏县| 开原市| 偃师市| 台东县| 塔城市| 台中县| 华亭县| 昌宁县| 娄底市| 普宁市| 彰化县| 广昌县| 方正县| 澎湖县| 长宁县| 金乡县| 仁怀市| 开封县| 丹巴县| 侯马市| 佛冈县| 精河县| 方山县| 家居| 涞水县| 舒兰市|