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"] = " ...
随机推荐
- UWP 图片剪切旋转工具
好久没撸随笔了,明天终于放假休息了..准备去进行信仰充值,看<魔兽>去(话说surface phone 好久出,让我这个做UWP的也充点信仰..) 先上下效果图: 在设计中,遇到一个问题, ...
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
自己接触ssm框架有一段时间了,从最早的接触新版ITOO项目的(SSM/H+Dobbu zk),再到自己近期来学习到的<淘淘商城>一个ssm框架的电商项目.用过,但是还真的没有自己搭建过, ...
- Tomcat中JVM内存溢出及合理配置及maxThreads如何配置(转)
来源:http://www.tot.name/html/20150530/20150530102930.htm Tomcat本身不能直接在计算机上运行,需要依赖于硬件基础之上的操作系统和一个Java虚 ...
- 关于vector的内存释放问题
以前一直想当然的以为vector 的clear()函数会保证释放vector的内存,今天网上一查资料发现完全不是我想象的那样子. 比如有如下代码: tempObject obj1; tempObjec ...
- 2015年ACM沈阳网络赛(准备做掉4道:)
Traversal Best Solver Minimum Cut Dividing This Product Excited Database Fang Fang Matches Puzzle Ga ...
- Subsonic的使用之基本语法、操作(2)
查询 SubSonic2.1版本 – 例出3种查询. Product product = new Select().From<Product>() .Where(Product.Produ ...
- node.js安装
Node.js是一个基于Chrome JavaScript运行时建立的一个平台,用来方便地搭建快速的,易于扩展的网络应用Node.js借助事件驱动,非阻塞I/O模型变得轻量和高效,非常适合run ac ...
- HDU3333 Turing Tree(线段树)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=3333 Description After inventing Turing Tree, 3x ...
- JQuery上传插件Uploadify使用详解
本文转载http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html Uploadify是JQuery的一个上传插件,实现的效果非常不错 ...
- [转] 《ES6标准入门》读书笔记
来源:https://segmentfault.com/a/1190000005863641 let和const命令 ES6新增let命令,用于声明变量,是块级作用域. let声明的变量不会像var声 ...