- Lua 教程
- Lua - 首页
- Lua 基础
- Lua - 概述
- Lua - 环境
- Lua - 基本语法
- Lua - 注释
- Lua - 打印 Hello World
- Lua - 变量
- Lua - 数据类型
- Lua - 运算符
- Lua - 循环
- Lua - 决策
- Lua - 函数
- Lua - 日期和时间
- Lua 数组
- Lua - 数组
- Lua - 多维数组
- Lua - 数组长度
- Lua - 迭代数组
- Lua 迭代器
- Lua - 迭代器
- Lua 列表
- Lua - 在列表中搜索
- Lua 模块
- Lua - 模块
- Lua - 命名空间
- Lua 元表
- Lua - 元表
- Lua 协程
- Lua - 协程
- Lua 文件处理
- Lua - 文件I/O
- Lua 库
- Lua - 标准库
- Lua - 数学库
- Lua - 操作系统功能
- Lua 有用资源
- Lua - 快速指南
- Lua - 有用资源
- Lua - 讨论
Lua - 表
简介
表是Lua中唯一可用的数据结构,它帮助我们创建不同类型的数据,例如数组和字典。Lua 使用关联数组,可以用数字和字符串(除了nil)作为索引。表没有固定大小,可以根据需要增长。
Lua 在所有表示中都使用表,包括包的表示。当我们访问方法 string.format 时,这意味着我们正在访问字符串包中可用的 format 函数。
表示和用法
表被称为对象,它们既不是值也不是变量。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
当我们有一个包含一组元素的表a,并将它赋值给b时,a和b都指向同一块内存。不会为b单独分配内存。当a设置为nil时,表仍然可以通过b访问。当没有引用指向一个表时,Lua 的垃圾回收机制会负责清理过程,以便重新使用这些未引用的内存。
示例
下面是一个例子,用于解释上述表的特性。
main.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
表操作
有一些内置函数用于表操作,它们列在下面的表中。
| 序号 | 方法和用途 |
|---|---|
| 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]) 根据可选的比较器参数对表进行排序。 |
让我们看一些上述函数的示例。
示例 - 表连接
我们可以使用 concat 函数连接两个表,如下所示:
main.lua
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
示例 - 插入和删除
在表中插入和删除项目是表操作中最常见的操作。解释如下。
main.lua
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
示例 - 排序表
我们经常需要按特定顺序对表进行排序。sort 函数按字母顺序对表中的元素进行排序。下面是一个示例。
main.lua
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