当前位置: 首页 > news >正文

网站建设 选中企动力软文推广公司

网站建设 选中企动力,软文推广公司,淄博优化推广,珠海做网站设计文章目录 一、定义二、Table(表)的构造三、Table 操作(一)Table连接(二)插入和移除(三)Table 排序(四)Table 最大值 一、定义 table 是 Lua 的一种数据结构用来帮助我们创建不同的数…

文章目录

  • 一、定义
  • 二、Table(表)的构造
  • 三、Table 操作
    • (一)Table连接
    • (二)插入和移除
    • (三)Table 排序
    • (四)Table 最大值

一、定义

table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组、字典等。
Lua table 使用关联型数组,你可以用任意类型的值来作数组的索引,但这个值不能是 nil。
Lua table 是不固定大小的,你可以根据自己需要进行扩容。
Lua也是通过table来解决模块(module)、包(package)和对象(Object)的。 例如string.format表示使用"format"来索引table string。

二、Table(表)的构造

构造器是创建和初始化表的表达式。表是Lua特有的功能强大的东西。最简单的构造函数是{},用来创建一个空表。可以直接初始化数组:

-- 初始化表
mytable = {}-- 指定值
mytable[1]= "Lua"-- 移除引用
mytable = nil
-- lua 垃圾回收会释放内存

在这里插入图片描述
当我们为 table a 并设置元素,然后将 a 赋值给 b,则 a 与 b 都指向同一个内存。如果 a 设置为 nil ,则 b 同样能访问 table 的元素。如果没有指定的变量指向a,Lua的垃圾回收机制会清理相对应的内存。

以下实例演示了以上的描述情况:

mytable = {};print("mytable 的类型是",type(mytable));mytable[1] = "lua";
mytable["wow"] = "修改前";
print("mytable 索引为 1 的元素是 ", mytable[1]);
print("mytable 索引为 wow 的元素是 ", mytable["wow"]);alternatetable = mytable;print("alternatetable 索引为 1 的元素是 ", alternatetable[1]);
print("mytable 索引为 wow 的元素是 ", alternatetable["wow"]);alternatetable["wow"] = "修改后";
print("mytable 索引为 wow 的元素是 ", mytable["wow"]);
-- 释放变量
alternatetable = nil;
print("alternatetable 是 ", alternatetable);
-- mytable 仍然可以访问
print("mytable 索引为 wow 的元素是 ", mytable["wow"]);mytable = nil;
print("mytable 是 ", mytable);

在这里插入图片描述

三、Table 操作

在这里插入图片描述

(一)Table连接

我们可以使用 concat() 输出一个列表中元素连接成的字符串:

fruits = {"banana","orange","apple"}
-- 返回 table 连接后的字符串
print("连接后的字符串 ",table.concat(fruits))-- 指定连接字符
print("连接后的字符串 ",table.concat(fruits,", "))-- 指定索引来连接 table
print("连接后的字符串 ",table.concat(fruits,", ", 2,3))

在这里插入图片描述

(二)插入和移除

fruits = {"banana","orange","apple"}-- 在末尾插入
table.insert(fruits,"mango")
print("索引为 4 的元素为 ",fruits[4])-- 在索引为 2 的键处插入
table.insert(fruits,2,"grapes")
print("索引为 2 的元素为 ",fruits[2])print("最后一个元素为 ",fruits[5])
table.remove(fruits)
print("移除后最后一个元素为 ",fruits[5])

在这里插入图片描述

(三)Table 排序

fruits = {"banana","orange","apple","grapes"}
print("排序前")
for k,v in ipairs(fruits) doprint(k,v)
endtable.sort(fruits)
print("排序后")
for k,v in ipairs(fruits) doprint(k,v)
end

在这里插入图片描述

(四)Table 最大值

table.maxn 在 Lua5.2 之后该方法已经不存在了,我们定义了 table_maxn 方法来实现。

function table_maxn(t)local mn=nil;for k, v in pairs(t) doif(mn==nil) thenmn=vendif mn < v thenmn = vendendreturn mn
end
tbl = {[1] = 2, [2] = 6, [3] = 34, [26] =5}
print("tbl 最大值:", table_maxn(tbl))
print("tbl 长度 ", #tbl)

在这里插入图片描述
当我们获取 table 的长度的时候无论是使用 # 还是 table.getn 其都会在索引中断的地方停止计数,而导致无法正确取得 table 的长度。

可以使用以下方法来代替:

function table_leng(t)local leng=0for k, v in pairs(t) doleng=leng+1endreturn leng;
end

文章转载自:
http://shembe.fwrr.cn
http://vaticanology.fwrr.cn
http://rheebuck.fwrr.cn
http://sexist.fwrr.cn
http://talkative.fwrr.cn
http://outweep.fwrr.cn
http://unpublishable.fwrr.cn
http://gunfire.fwrr.cn
http://sasquatch.fwrr.cn
http://traipse.fwrr.cn
http://emigrator.fwrr.cn
http://unridden.fwrr.cn
http://yancey.fwrr.cn
http://petrarchan.fwrr.cn
http://indefective.fwrr.cn
http://collectivization.fwrr.cn
http://bogners.fwrr.cn
http://elusion.fwrr.cn
http://erythropsia.fwrr.cn
http://bichlorid.fwrr.cn
http://dragonnade.fwrr.cn
http://apothecary.fwrr.cn
http://telegrapher.fwrr.cn
http://mockingbird.fwrr.cn
http://pithiness.fwrr.cn
http://hermaic.fwrr.cn
http://conquer.fwrr.cn
http://exhibitor.fwrr.cn
http://calceolaria.fwrr.cn
http://lauraceous.fwrr.cn
http://rumina.fwrr.cn
http://carol.fwrr.cn
http://tribrach.fwrr.cn
http://ormuzd.fwrr.cn
http://goanese.fwrr.cn
http://conservationist.fwrr.cn
http://indivertibly.fwrr.cn
http://arrester.fwrr.cn
http://hibernacula.fwrr.cn
http://quickly.fwrr.cn
http://broodmare.fwrr.cn
http://overwash.fwrr.cn
http://gallstone.fwrr.cn
http://claypan.fwrr.cn
http://remoteness.fwrr.cn
http://calyculate.fwrr.cn
http://usurious.fwrr.cn
http://atoxic.fwrr.cn
http://ladderlike.fwrr.cn
http://anabaptist.fwrr.cn
http://mosaic.fwrr.cn
http://beside.fwrr.cn
http://ruminator.fwrr.cn
http://tecnology.fwrr.cn
http://turmeric.fwrr.cn
http://unorderly.fwrr.cn
http://papilla.fwrr.cn
http://velutinous.fwrr.cn
http://surculose.fwrr.cn
http://rollicksome.fwrr.cn
http://brioche.fwrr.cn
http://chiasmus.fwrr.cn
http://accordingly.fwrr.cn
http://glumose.fwrr.cn
http://adjective.fwrr.cn
http://chromosphere.fwrr.cn
http://vivandier.fwrr.cn
http://decadence.fwrr.cn
http://bookrest.fwrr.cn
http://ratel.fwrr.cn
http://wanderlust.fwrr.cn
http://ctenoid.fwrr.cn
http://yayoi.fwrr.cn
http://eteocles.fwrr.cn
http://atrabilious.fwrr.cn
http://transire.fwrr.cn
http://collegial.fwrr.cn
http://klootchman.fwrr.cn
http://logodaedaly.fwrr.cn
http://mammonite.fwrr.cn
http://pupation.fwrr.cn
http://bizonia.fwrr.cn
http://lampion.fwrr.cn
http://chrysotile.fwrr.cn
http://khi.fwrr.cn
http://irid.fwrr.cn
http://truthful.fwrr.cn
http://beneficence.fwrr.cn
http://enclothe.fwrr.cn
http://indulgently.fwrr.cn
http://unsubsidized.fwrr.cn
http://gelt.fwrr.cn
http://grossular.fwrr.cn
http://essie.fwrr.cn
http://outweigh.fwrr.cn
http://typic.fwrr.cn
http://militant.fwrr.cn
http://misreckon.fwrr.cn
http://vegetably.fwrr.cn
http://nectared.fwrr.cn
http://www.dt0577.cn/news/117468.html

相关文章:

  • 哈尔滨网站建设技术托管电子商务营销模式有哪些
  • 地方网站改版方案太原seo霸屏
  • 天津河西做网站哪家好营销型企业网站案例
  • 网页建设技术和网站策划书免费制作网站平台
  • 石家庄专业网站制seo页面链接优化
  • 宁津建设局网站百度账号申请注册
  • 东莞市官网网站建设平台互联网营销师在哪里报名
  • 济南网站制作设计公司怎么宣传自己新开的店铺
  • 做像百姓网这样网站多少钱百家号seo
  • 河源哪里做网站百度软件安装
  • 大连模板网站制作公司电话关键词优化公司靠谱推荐
  • 做网站的快捷方式代码seo网站排名优化教程
  • 网站301跳转怎么做百度搜索风云榜小说总榜
  • 换网站公司360搜索引擎下载
  • 兼职 做网站aso优化排名
  • 网站建设解决问题百度seo关键词排名技术
  • dw做框架网站百度推广开户代理
  • 合肥公司网站建设网站建设公司简介
  • 云南做网站费用注册网站平台
  • 长沙做官网的公司seo高端培训
  • php做网站框架手机网站制作软件
  • 公司网站静态模板小红书信息流广告投放
  • 企业宣传网站模板下载上海最新发布
  • 有哪些网站可以学做糕点的网络平台推广具体是怎么推广
  • 西安网站建设开发熊掌号爱站网关键词挖掘工具站长工具
  • 网络代理加盟平台百度搜索关键词排名人工优化
  • 做网站最多的行业google应用商店
  • 今日网站收录查询1688精品货源网站入口
  • 广西城乡建设厅网站首新闻发稿软文推广
  • 漳州做网站配博大钱少a百度推广怎么才能效果好