国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Ruby > 正文

詳解Lua中的表的概念及其相關(guān)操作方法

2020-02-24 15:37:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

表是唯一的數(shù)據(jù)結(jié)構(gòu),Lua可以幫助我們創(chuàng)建不同的類(lèi)型,Lua使用關(guān)聯(lián)數(shù)組,不僅使用數(shù)字,還使用不同的零字符串索引,武林技術(shù)頻道小編詳解Lua中的表的概念及其相關(guān)操作方法,一起來(lái)看看吧!

Lua采用的所有陳述,包括包裝的代表性表。當(dāng)我們?cè)L問(wèn)一個(gè)方法的字符串。格式,這意味著,我們正在訪(fǎng)問(wèn)的格式化功能的字符串封裝。
表示和用法

表稱(chēng)為對(duì)象和它們既不值,也沒(méi)有變。 Lua使用構(gòu)造函數(shù)表達(dá)式{}創(chuàng)建一個(gè)空表。它是要知道,有保存表的參考和表本身的變量之間沒(méi)有固定的關(guān)系。

 

復(fù)制代碼 代碼如下:
--sample table initialization
mytable = {}

 

--simple table value assignment
mytable[1]= "Lua"

--removing reference
mytable = nil
-- lua garbage collection will take care of releasing memory

 

當(dāng)我們有一個(gè)表與集合的元素,如果我們將其指定為b,a和b都指向相同的內(nèi)存。沒(méi)有單獨(dú)的內(nèi)存單獨(dú)分配對(duì)b。當(dāng)設(shè)置為無(wú),表將仍然可以訪(fǎng)問(wèn)到b。當(dāng)沒(méi)有引用表,然后在Lua垃圾收集需要清理過(guò)程,使這些未引用的內(nèi)存再次被重用。

一個(gè)例子如下所示用于說(shuō)明表的上述特征。

 

復(fù)制代碼 代碼如下:
-- 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)

 

當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出

 

復(fù)制代碼 代碼如下:
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

 

表操作

在對(duì)表操作內(nèi)置函數(shù)和它們被列于下表中。

 讓我們看看上面的函數(shù)一些例子。
表串聯(lián)

我們可以使用concat函數(shù)來(lái)連接,如下所示的兩個(gè)表。

 

復(fù)制代碼 代碼如下:
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))

 

當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出

 

復(fù)制代碼 代碼如下:
Concatenated string  bananaorangeapple
Concatenated string  banana, orange, apple
Concatenated string  orange, apple

 

插入和刪除

插入在表中的項(xiàng)目,并除去最常見(jiàn)于表操縱。它下面的解釋。

 

復(fù)制代碼 代碼如下:
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])

 

當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出

 

復(fù)制代碼 代碼如下:
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

 

排序表格

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

 

復(fù)制代碼 代碼如下:
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

 

當(dāng)我們運(yùn)行上面的程序,會(huì)得到下面的輸出

 

復(fù)制代碼 代碼如下:
1 banana
2 orange
3 apple
4 grapes
sorted table
1 apple
2 banana
3 grapes
4 orange

以上就是武林技術(shù)頻道給大家介紹的詳解Lua中的表的概念及其相關(guān)操作方法,如若您想要學(xué)習(xí)更多關(guān)于技術(shù)方面的知識(shí)可以進(jìn)入武林技術(shù)頻道網(wǎng)學(xué)習(xí),最后預(yù)祝大家學(xué)習(xí)愉快。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 西贡区| 韶关市| 米脂县| 宜兰市| 花莲市| 德安县| 宁南县| 青铜峡市| 普定县| 普兰县| 浦北县| 西乌珠穆沁旗| 苏尼特右旗| 景谷| 五家渠市| 海门市| 青海省| 浙江省| 湖北省| 成都市| 贡觉县| 横山县| 郑州市| 鄂托克旗| 九台市| 滁州市| 保定市| 定南县| 武宁县| 都匀市| 铜陵市| 深泽县| 开江县| 庆元县| 渭源县| 庆元县| 大英县| 称多县| 云浮市| 昆山市| 合水县|