lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?

RT
local t = {1,888,x= 999,b=2,5,nil,6,7,[10]=1,8,{z = 1,y = 1},nil,nil}

print(table.getn(t))
print(#t)
print(table.maxn(t))

输出:8 8 8
------------如果把[10] =1 改成[11] =1 那么输出8 8 11 这又是为什么?

 
 
 
1个回答
 
==1==

table.getn(t)
等价于 #t
但是它计算的是数组元素。不包括hash 键值。
而且数组是以第一个nil元素来判断数组结束。
#只计算array的元素个数,它实际上调用了对象的metatable 的__len函数
对于有__len 方法的函数返回函数返回值。不然就返回数组成员数目。
==2==

a={1,3,a='b',[6]='six',['10']='ten'}
a 和 [6] ['10']是作为hash保存的。#a => 2 他是不包括hash成员的计数。
1 3 是 数组结构保存的。table.maxn(a) => 6
因为a中所有元素最大的数值索引是6不是字符串10

.你的代码返回11 是因为它是最大的数值索引。
maxn lua 5.2 已经抛弃了,不过依然可以使用。

更多追问追答

 
 
追问
但是它计算的是数组元素。不包括hash 键值。
而且数组是以第一个nil元素来判断数组结束。 =========这个意思不就是说get(n)只会计算到第一个nil就停止么,但是我上面的例子好像没有停止,
 
追答
a={1,3,a='b',[6]='six',['10']='ten'}
a 和 [6] ['10']是作为hash保存的。#a => 2 他是不包括hash成员的计数。
这句话我说的有点误会。其实数组的数据结构也是hash。lua中数组的意思是:table的元素拥有连续的数字索引。比如:{[1]=1,[2]=2,[3]=3} The length operator is denoted by the unary prefix operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).
A program can modify the behavior of the length operator for any value but strings through the __len metamethod (see §2.4).
Unless a __len metamethod is given, the length of a table t is only defined if the table is a sequence, that is, the set of its positive numeric keys is equal to {1..n} for some integer n. In that case, n is its length. Note that a table like
{10, 20, nil, 40}
is not a sequence, because it has the key 4 but does not have the key 3. (So, there is no n such that the set {1..n} is equal to the set of positive numeric keys of that table.) Note, however, that non-numeric keys do not interfere with whether a table is a sequence.
上面这句话是手册里的内容。
table的长度是 table为序列时的某个n 使得,{1..n} == table的所有数字索引。也就是说序列的数字索引必须连续。
# 运算得到的length 只针对 序列有意义 。当table不是序列时length 没有明确含义。
table中间包括nil,这个table就不是序列,例如:
a={10, 20, nil, 40} 它不是序列,因为他的数字索引是 1 2 4 不是连续的,所以它不是序列。
所以 #a 得到的值并不能准确反映table的元素个数。
手册的意思是说 针对非序列使用#,而那个table又没有__len 时它值无意义的。 再给你个实例:
a={1,2,3,4,5}
print(#a)
5
a[2]=nil
print(#a) --此时a已经不是序列,此时对数列使用# , 具体意义不明。
5
a[5]=nil --删除最后元素
print(#a) --看看 结果。
1
===
对于你想删除某个序列的元素,必须使用 table.remove 而不是 a[n]=nil

lua中 table.getn(t) 、#t、 table.maxn(t) 这三个什么区别?的更多相关文章

  1. Lua中的weak表——weak table

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  2. Lua中的weak表——weak table(转)

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  3. lua中,两种json和table互转方法的效率比较

    lua中json和table的互转,是我们在平时开发过程中经常用到的.比如: 在用lua编写的服务器中,如果客户端发送json格式的数据,那么在lua处理业务逻辑的时候,必然需要转换成lua自己的数据 ...

  4. Lua中的元表与元方法

    [前言] 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了“+”符号,就可以进行类的加法运算.在Lua中也有这个道理 ...

  5. Lua中的一些库(1)

    [数学库] 数学库(math)由一组标准的数学函数构成.这里主要介绍几个常用的函数,其它的大家可以自行百度解决. 三角函数(sin,cos,tan……)所有的三角函数都使用弧度单位,可以用函数deg( ...

  6. Lua中的metatable详解

    转自:http://www.jb51.net/article/56690.htm Lua 中 metatable 是一个普通的 table,但其主要有以下几个功能: 1.定义算术操作符和关系操作符的行 ...

  7. Lua中的元表与元方法学习总结

    前言 元表对应的英文是metatable,元方法是metamethod.我们都知道,在C++中,两个类是无法直接相加的,但是,如果你重载了"+"符号,就可以进行类的加法运算.在Lu ...

  8. Lua中的table函数库

    table.concat(table, sep,  start, end) concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组 ...

  9. lua中求table长度

    关于lua table介绍,看以前的文章http://www.cnblogs.com/youxin/p/3672467.html. 官方文档是这么描述#的: 取长度操作符写作一元操作 #. 字符串的长 ...

随机推荐

  1. centos 7 增加永久静态路由

    1 在 /etc/sysconfig/network-scripts/ 目录下添加route-eth3,eth3为实际网卡名称. [root@compute1 ~]# cat /etc/sysconf ...

  2. java使字符串的数字加一

    /** * 字符串+1方法,该方法将其结尾的整数+1,适用于任何以整数结尾的字符串,不限格式,不限分隔符. * @author zxcvbnmzb * @param testStr 要+1的字符串 * ...

  3. 你想了解的 HTTPS 都在这里

    HTTP 协议仅仅制定了互联网传输的标准,简化了直接使用 TCP 协议进行通信的难度.有关 HTTP 协议相关的讲解请看前面两节: HTTP 协议详解 HTTP协议详解(二) less is more ...

  4. 一个ACE 架构的 C++ Timer

    .h #ifndef _Timer_Task_ #define _Timer_Task_ #pragma once #include <ace/Task.h> #include <a ...

  5. CenterOS的安装配置(配图解)

    CenterOS的安装配置 一.    配置虚拟机 打开Virtual Machine(虚拟机),点击create new virtual machine 进入新建虚拟机向导页面,选择[Custom( ...

  6. 微信h5页面下拉露出网页来源的解决办法

    微信h5页面下拉露出网页来源的解决办法:将document的touchmove事件禁止掉 //禁止页面拖动 document.addEventListener('touchmove', functio ...

  7. Mister B and PR Shifts,题解

    题目链接 分析: 题意很明白,不再多说了,直接分析题目,首先想一想暴力,直接枚举起点,然后求出来,时间复杂度n*n,显然不太好,所以我们考虑换一种方法枚举,当然本质还是枚举,其实你会发现变化i次和i+ ...

  8. Aaronson,又是思维题

    题目: Recently, Peter saw the equation x0+2x1+4x2+...+2mxm=nx0+2x1+4x2+...+2mxm=n. He wants to find a ...

  9. mac篇---使用iTerm2快捷连接SSH

    大家都知道使用iTerm2连接shh 使用命令 ssh -p22 root@129.10.10.1,然后输入密码即可. 但是每次都输入还是比较麻烦的.iTerm2为我们提供了快捷的方式.三步即可完成此 ...

  10. 查看windows操作系统的默认编码

    转自:https://blog.csdn.net/zp357252539/article/details/79084480/ 在Windows平台下,进入DOS窗口,输入:chcp 可以得到操作系统的 ...