好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 詳解Lua中的表的概念及其相關操作方法

詳解Lua中的表的概念及其相關操作方法

熱門標簽:常德電銷平臺外呼系統軟件價格 湖州u友防封電銷卡 地圖標注賺錢項目注冊 百度地圖標注自定義圖片 電銷機器人廠商代理 滴滴外呼系統 徐州網絡外呼系統哪個好 高德地圖標注客服 白銀外呼paas系統

 表格是唯一的數據結構中Lua可以幫助我們創造出不同的類型,如數組和字典。 Lua使用關聯數組和可不僅數字,但也有不同的零字符串索引。表格都沒有固定的大小,并根據需要可以增長。

Lua采用的所有陳述,包括包裝的代表性表。當我們訪問一個方法的字符串。格式,這意味著,我們正在訪問的格式化功能的字符串封裝。
表示和用法

表稱為對象和它們既不值,也沒有變。 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

當我們有一個表與集合的元素,如果我們將其指定為b,a和b都指向相同的內存。沒有單獨的內存單獨分配對b。當設置為無,表將仍然可以訪問到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

表操作

在對表操作內置函數和它們被列于下表中。

 讓我們看看上面的函數一些例子。
表串聯

我們可以使用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

插入和刪除

插入在表中的項目,并除去最常見于表操縱。它下面的解釋。

復制代碼 代碼如下:
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

排序表格

排序表通常需要和排序函數表中的元素按字母順序排序。下圖所示為這方面的一個范例。

復制代碼 代碼如下:
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


您可能感興趣的文章:
  • Lua中的元表與元方法學習總結
  • Lua中的元表(metatable)、元方法(metamethod)詳解
  • 獲取Lua表結構(table)數據實例

標簽:普洱 荊門 張家界 梧州 三沙 公主嶺 遼寧 永州

巨人網絡通訊聲明:本文標題《詳解Lua中的表的概念及其相關操作方法》,本文關鍵詞  詳解,Lua,中的,表,的,概念,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《詳解Lua中的表的概念及其相關操作方法》相關的同類信息!
  • 本頁收集關于詳解Lua中的表的概念及其相關操作方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 国产精品久久久久久无码AV片| 成人国产精品| 深夜国产福利| 中国jiZZ女人高潮完整版Free| 啊轻点灬大ji巴太长太粗小说| 情涩导航| 奶大灬舒服灬一进一出三区| 97精品人妻无码专区| 亚洲日本欧美产综合在线| 日本a视频| 激情综合网五月激情| 免费一级婬片A片AAA毛片小说| 男女插下面视频| 男女爽爽爽视频| 天天插天天爽| 91久久久精品国产一区二区蜜臀| 国产人妻绿帽3p国语对白| 99爱在线精品视频网站| 五月婷婷激情四射| 乱色国产熟妇一区二区| 风间由美亚洲一区二区三区| 高清激情三级视频| 寡妇门前桃花多| 午夜日韩电影| 一边吃奶一边添p好爽视频第9季| 看的免费污污网站| 国产激情自拍| 人妻含泪让粗大挺进| 国产AV一区二区三区| 无码帝国www色情综合| 喜欢被老男人吃奶摸下面| 男生爆操女生| 使劲灬把麻麻弄爽了在线观看| 国产亚洲精品久久久久久精品桃花岛 | 日本黄区免费视频观看| 欧美色99| 公主在野外被3p| 女人扒下裤让男人桶到爽| 好疼轻点太紧了男男| 歪歪漫画sss韩漫画在线| 日本无遮挡吸乳大尺度|