麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Ruby > 正文

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

2020-02-24 15:37:51
字體:
來源:轉載
供稿:網友

表是唯一的數據結構,Lua可以幫助我們創建不同的類型,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中的表的概念及其相關操作方法,如若您想要學習更多關于技術方面的知識可以進入武林技術頻道網學習,最后預祝大家學習愉快。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 日本成人高清视频 | 亚洲成人高清在线观看 | 国产精品成人一区二区三区吃奶 | 7777在线视频免费播放 | 亚洲欧美一区二区三区在线观看 | 免费国产在线视频 | 欧美a区| 一级做a爱片性色毛片 | 国产羞羞视频在线观看 | 欧美另类在线视频 | 97青青草视频 | 中国av免费在线观看 | 久久毛片免费观看 | 免费成人 | 成人福利在线看 | 亚洲精品永久视频 | 久久久久中精品中文字幕19 | 国产精品毛片无码 | 久久亚洲成人 | 久久99精品国产99久久6男男 | 在线观看视频亚洲 | 久久综合入口 | 精品成人网| 成年免费视频黄网站在线观看 | 男女羞羞视频 | 久久69精品久久久久久国产越南 | 国产精品亚洲三区 | 欧美特黄特色视频 | 99影视在线视频免费观看 | 操网| 午夜亚洲视频 | 久久久久久亚洲综合影院红桃 | 高清国产午夜精品久久久久久 | 国产精品1区,2区,3区 | 久久精品欧美电影 | 欧美电影在线观看 | 久草在线综合 | 男人的天堂视频网站 | 美女毛片在线观看 | 婷婷久久青草热一区二区 | 欧美成人精品一区二区三区 |