lua table integer index 特性
table.maxn (table)
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. (To do its job this function does a linear traversal of the whole table.)
返回表中最大的正数值index。
说明:
1、 此接口不是统计表中元素的数目。
2、 如果表中 positive numerical index 不是连续的, 12 4, 缺少3为index的元素, 计算值为 maxn为 4。
local test = {}
table.insert(test, {})
table.insert(test, , {})
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
table.maxn(test)=4
i=1 v=1
i=2 v=table: 00559670
i=4 v=table: 005597B0
FOR IPAIRS vs Non Positive Integer Index
对于不连续的 positive integer index情况, 如果使用 ipairs 迭代器, 只能获取第一个连续的index段。
故不能遍历所有元素, 如果需要则使用 pairs迭代器。
local test = {}
table.insert(test, {})
table.insert(test, , {})
print("table.maxn(test)="..table.maxn(test))
for i,v in ipairs(test) do
print("i="..i.." v="..tostring(v))
end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
table.maxn(test)=4
i=1 v=1
i=2 v=table: 00339580
table.remove (table [, pos])
Removes from
tablethe element at positionpos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value forposisn, wherenis the length of the table, so that a calltable.remove(t)removes the last element of tablet.
删除pos位置元素, 并将左边的高index元素向左移动一个位置。 此处所指的高index是指左侧所有的 元素, 故 maxn值会减少1.
local test = {}
table.insert(test, {})
table.insert(test, , {})
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
table.remove(test, )
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
table.maxn(test)=4
i=1 v=1
i=2 v=table: 00589350
i=4 v=table: 005892B0
table.maxn(test)=3
i=1 v=table: 00589350
i=3 v=table: 005892B0
>Exit code: 0
table.insert (table, [pos,] value)
Inserts element
valueat positionposintable, shifting up other elements to open space, if necessary. The default value forposisn+1, wherenis the length of the table (see §2.5.5), so that a calltable.insert(t,x)insertsxat the end of tablet.
将元素value插到pos位置, 此位置以及以上的元素都向右边移动一位。 会将maxn值加1.
local test = {}
table.insert(test, {})
table.insert(test, , {})
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
table.insert(test, , )
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
table.maxn(test)=4
i=1 v=1
i=2 v=table: 00979800
i=4 v=table: 009795F8
table.maxn(test)=5
i=1 v=33
i=2 v=1
i=3 v=table: 00979800
i=5 v=table: 009795F8
>Exit code: 0
table.remove vs nil set
nil 不会做 将右边的元素向左移动一位的动作, remove会。
local test = {}
table.insert(test, {})
table.insert(test, , {})
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
test[] = nil
print("table.maxn(test)="..table.maxn(test))
for i,v in pairs(test) do
print("i="..i.." v="..tostring(v))
end
LOG:
>lua -e "io.stdout:setvbuf 'no'" "luatest.lua"
table.maxn(test)=4
i=1 v=1
i=2 v=table: 003D9508
i=4 v=table: 003D9580
table.maxn(test)=4
i=2 v=table: 003D9508
i=4 v=table: 003D9580
>Exit code: 0
lua table integer index 特性的更多相关文章
- Lua table使用
days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Th ...
- 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...
- 9.6 翻译系列:数据注解之Index特性【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/entityframework6/index-attribute-in-code-first.aspx EF ...
- 【转载】【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
[游戏开发]在Lua中实现面向对象特性——模拟类.继承.多态 阅读目录 一.简介 二.前提知识 三.Lua中实现类.继承.多态 四.总结 回到顶部 一.简介 Lua是一门非常强大.非常灵活的脚本语 ...
- 树形打印lua table表
为方便调试lua程序,往往想以树的形式打印出一个table,以观其表内数据.以下罗列了三种种关于树形打印lua table的方法;法一 local print = print local tconca ...
- lua table 排序--满足多条件排序
前提 假设 一个小怪 有三种属性,等级(level).品质(quality).id(pid) 我们需要对他们进行排序,两种排序情况,第一是单一属性排序,比如按照等级进行排序,或者多种属性进行优先级排序 ...
- cocos2d-x lua table数据存储
cocos2d-x lua table数据存储 version: cocos2d-x 3.6 1. 将table转为json http://blog.csdn.net/songcf_faith/art ...
- cocos2d-x lua table与json的转换
cocos2d-x lua table与json的转换 version: cocos2d-x 3.6 1.引入json库 require("src/cocos/cocos2d/json&qu ...
- lua table表
lua table表 语法结构 创建一个 table 直接使用 "{}" 即可 table1 = {} -- 赋值 table1["name"] = " ...
随机推荐
- IBatis.Net使用总结(三)-- IBatis实现分页返回数据和总数
IBatis 分页,这里没有使用其他插件,只使用最原始的方法. 输入参数: int currentPage 当前页 int pageSize 每页大小 Hashtable findCondition ...
- ubuntu下配置jdk
1.首先下载jdk-7u51-linux-i586.tar.gz.并将它放在例如/home目录. 2.解压安装 sudo tar zxvf ./jdk-7u51-linux-i586.tar.gz ...
- Install MySQL on CentOS 7
原文:https://devops.profitbricks.com/tutorials/install-mysql-on-centos-7/ 1.下载mysql 在mysql官网选择适合的mysql ...
- 使用samba实现linux与windows共享(测试成功)
samba服务器搭建 实现linux与windows文件共享有很多种方法,诸如wpc,vmtools等,今天我来介绍我在rehat系统中实验成功并且现在在用的的方法,直接给出操作步骤: ...
- Daily Scrum Meeting ——FifthDay
一.Daily Scrum Meeting照片 牛姐去工程师那边了,已经在群里给我汇报了.橙汁去北京参加ICPC了 二.Burndown Chart 渐入佳境了,最后一礼拜了 三.项目进展 1.实现注 ...
- mysql导入导出,及错误记录
进入mysql的bin目录,如果mysql的bin添加了环境变量则不用. 导出,不指定编码则默认为:utf8mb4.: mysqldump -u root -h 127.0.0.1 -P 3307 - ...
- 朋友圈常见单页面触屏滑动上下翻屏功能jQuery实现
翻页插件:实现原理,用margin-top来控制页面容器位置来实现上下翻页.margin这属性很好用,可以用来制作侧栏动画滑出菜单(左菜单,右内容,控制两者的margin实现):或者head下滑菜单 ...
- 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
- 自定义RadioButton样式
一,在RadioButton标签上使用 android:button="@drawable/pay_radio_selector" 可以修改按钮的样式 二,在RadioButton ...
- Leetcode Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...