Lua 基础教程(十三)表

本文介绍了Lua中的表数据结构,包括其作为关联数组的特性、动态增长、表的初始化、赋值与引用、以及内置函数如table.concat、table.insert、table.remove和table.sort的使用示例。
摘要由CSDN通过智能技术生成

Lua 基础教程(十三)表

介绍

在 Lua 中,表(Tables)是唯一可用的数据结构,可帮助我们创建不同类型的数组和字典。Lua 使用关联数组,不仅可以用数字索引,还可以用非nil的字符串索引。表大小不固定,可以根据需要动态增长。

Lua 在所有的表示中都使用表,包括用表表示包。当访问 string.format 方法时,实际上是在访问字符串包中的 format 函数。

表示和用法

表被称为对象,它们既不是值也不是变量。Lua 使用构造表达式 {} 来创建空表。需要知道的是,变量持有表的引用与表本身之间没有固定的关系。

-- 示例表的初始化
mytable = {}

-- 简单的表值赋值
mytable[1] = "Lua"

-- 移除引用
mytable = nil

-- Lua 的垃圾回收机制会释放内存

当表 a 具有一组元素并将其赋值给 b 时,ab 都引用相同的内存。对 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

表的操作

Lua有一些内置函数用于表操作,如下列表:

序号方法和用途
1table.concat (table [, sep [, i [, j]]])
基于给定的参数连接表中的字符串。详细信息请参见示例。
2table.insert (table, [pos,] value)
在指定位置将值插入表中。
3table.maxn (table)
返回最大的数字索引。
4table.remove (table [, pos])
从表中移除值。
5table.sort (table [, comp])
基于可选的比较器参数对表进行排序。

下面看一些上述函数的示例。

表拼接

可以使用 concat 函数将两个表拼接起来,示例如下:

fruits = {"banana", "orange", "apple"}

-- 返回表的连接字符串
print("Concatenated string ", table.concat(fruits))

-- 用字符连接
print("Concatenated string ", table.concat(fruits, ", "))

-- 基于索引连接水果
print("Concatenated string ", table.concat(fruits, ", ", 2, 3))

运行上述程序时,会得到以下输出:

Concatenated string    bananaorangeapple
Concatenated string    banana, orange, apple
Concatenated string    orange, apple

插入和删除

在表中插入和删除项是最常见的操作。如下代码所示:

fruits = {"banana", "orange", "apple"}

-- 在末尾插入水果
table.insert(fruits, "mango")
print("Fruit at index 4 is ", fruits[4])

-- 在索引 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

对表进行排序

我们经常需要按特定顺序对表进行排序。sort 函数根据字母表顺序对表中的元素进行排序。示例如下 :

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值