Lua - Tables(表)

Lua - Tables(表) 首页 / Lua入门教程 / Lua - Tables(表)

表是Lua中唯一可帮助无涯教程创建数组和字典等不同类型的数据结构。 Lua使用关联数组,不仅可以用数字索引,还可以用字符串(除nil外)进行索引。Tables没有固定的大小,可以根据需要增加。

Tables 用法

Tables称为对象,它们既不是值也不是变量。 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

当有一个包含元素集的表table a 并将其分配给 b 时, a 和 b 引用相同的内存。没有为b单独分配单独的内存。当a设置为nil时,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("alternatetable 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
alternatetable 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

Tables 操作

内置了用于表操作的函数,这些函数在下表中列出。

Sr.No.Method & Purpose
1

table.concat(Table[,sep [,i [,j]]])

根据给定的参数连接表中的字符串。

2

table.insert(Table,[pos,] value)

在表中的指定位置插入一个值。

3

table.maxn(table)

返回最大的数字索引。

4

table.remove(table [,pos])

从表中删除值。

5

table.sort(table [,comp])

根据可选的比较器参数对表进行排序。

Tables 表串联

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

插入和移除

在表操作中,表项的插入(insert)和删除(Remove)是最常见的。下面说明。

链接:https://www.learnfk.comhttps://www.learnfk.com/lua/lua-tables.html

来源:LearnFk无涯教程网

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

Tables排序

经常需要按特定顺序对表格进行排序(Sorting Tables)。如下所示。

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构核心20讲 -〔杨波〕

Java核心技术面试精讲 -〔杨晓峰〕

程序员的数学基础课 -〔黄申〕

10x程序员工作法 -〔郑晔〕

消息队列高手课 -〔李玥〕

设计模式之美 -〔王争〕

技术管理案例课 -〔许健〕

说透5G -〔杨四昌〕

大厂设计进阶实战课 -〔小乔〕

好记忆不如烂笔头。留下您的足迹吧 :)