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. python计算图像信息熵

    import cv2 import numpy as np import math import time def get_entropy(img_): x, y = img_.shape[0:2] ...

  2. webpack入门进阶(3)

    1.11.预处理器文件处理 1.sass文件 sass这种css预处理器是以.scss结尾,需要用node-sass和sass-loader来处理 安装loader npm i node-sass s ...

  3. nginx 注释配置及详解

    前言 DMZ(Demilitarized Zone) 非军事区,生产环境 WEB 服务部署的区域,公司的架构为一台nginx 充当 load balance 服务,负载到两台 nginx 上面,反向代 ...

  4. java.math.BigDecimal cannot be cast to [Ljava.lang.Object;

    从数据库中使用sum函数取出统计值后,放进list中,遍历list的时候强转化成Object是报错. BigDecimal .Integer不是基本类型,是int的包装类,无法把包装当做基本类型来用. ...

  5. 手写SpringMVC框架(一)-------项目搭建

    SpringMVC处理请求的大致流程: 我们来开始着手手写一个SpringMVC框架. 新建一个springMVC项目,流程参见 SpringMVC框架搭建流程 引入servlet相关的jar包: & ...

  6. ExecutorsService 中的 submit和 execute的区别

    闲来无事,写点代码练练手.于是就看了下ExecutorService常用的提交任务的方法: <T> Future<T> submit(Callable<T> tas ...

  7. Docker镜像-列出镜像

    列出镜像 镜像体积 虚悬镜像 中间层镜像 列出部分镜像 要想列出已经下载下来的镜像,可以使用docker images 或者 docker image ls 命令. $ docker image ls ...

  8. 深入Mybatis源码——配置解析

    @ 目录 前言 正文 配置解析 1. cacheRefElement/cacheElement 2. resultMapElements 3. sqlElement 4. buildStatement ...

  9. Dot Net Core中间件内部运行机制

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { ...

  10. UiAutomator源码学习(2)-- UiAutomationBridge

    从上一章对UiDevice的学习,可以看出几乎所有的操作都离不开 UiAutomationBridge.重新看一下UIDevice的构造方法: private UiDevice(Instrumenta ...