lua的数据结构
发布时间:2020-12-14 22:26:11 所属栏目:大数据 来源:网络整理
导读:lua的数据结构 table是Lua中唯一的数据结构,其他语言所提供的其他数据结构比如:arrays、records、lists、queues、sets等,Lua都是通过table来实现。 ? 数组: a = {} -- new array for i=1,1000 do ??? a[i] = 0 end? ? 下标可以从任意数值开始 比如: arr
lua的数据结构
table是Lua中唯一的数据结构,其他语言所提供的其他数据结构比如:arrays、records、lists、queues、sets等,Lua都是通过table来实现。
?
数组:
a = {} -- new array
for i=1,1000 do ??? a[i] = 0 end?
?
下标可以从任意数值开始
比如:
arr = {}
for a=0.1,10,0.1 do ?arr[a] = a*123.456 ?print (arr[a]) end
lua习惯下标从1开始,这样可以使用标准库
?
矩阵和多维数组:
?
每行一个table,
mt = {} -- create the matrix
for i=1,N do ? mt[i] = {} -- create a new row ? for j=1,M do ??? mt[i][j] = 0 ? end end
?
所有数据在一个table中
mt = {} -- create the matrix
for i=1,N do ? for j=1,M do ??? mt[i*M + j] = 0 ? end end
?
?
链表:
list = nil
在list前插入一个元素:
list = {next = list,value = v}
?
遍历:
local l = list ? while l do ??? print(l.value) ??? l = l.next ??end
?队列:
List = {}
function List.new () ? return {first = 0,last = -1} end
左侧添加,first减1,左侧删除first加1,
右侧添加,last加1,右侧删除last减1,
first大于last则表示队列空
?
集合:
这是一个关键字的集合:
reserved = {
["while"] = true,["end"] = true, ["function"] = true,["local"] = true, }
说明:集合的元素是table的键,而不是值。
原始集合是:{"while","end","function","local",} ?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |