微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

lua的数据结构

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 @H_404_49@end
 
所有数据在一个table中
mt = {} -- create the matrix
for i=1,N do
  for j=1,M do @H_404_58@    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",}  

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐