table库
来源:互联网 发布:c语言编写图形 编辑:程序博客网 时间:2024/06/17 00:58
一、table.insert()
1) t={10,20,30}
table.insert(t,1,15) --(表名,位置,值) -> t={15,10,20,30}
2) table.insert(t,15) --(表名,值) -> t={10,20,30,15} 默认插在表末
二、table.remove()
1) t={10,20,30}
table.remove(t,2) --(表名,位置) -> t={10,30}
2) table.remove(t) --(表名) -> t={10,20} 默认删除表末
三、table.sort()
原理就是LUA集成的冒泡算法,应用table.sort(表名)。
--排序的算法
function comps(a,b)
return a.id < b.id
end
1) mytable={1,2,5,4}
table.sort(mytable) --mytable={1,2,4,5}
2) mytable={"aht", "my", "cool"}
table.sort(mytable) --mytable={"aht","cool","my"} 注意:数字和字符是不能混合排序的
3)键值对Table排序(按Key排序,不是Value)
- local test_table = {a=3,b=2,c=4,d=1}
- local key_table = {}
- --取出所有的键
- for key,_ in pairs(test_table) do
- table.insert(key_table,key)
- end
- --对所有键进行排序
- table.sort(key_table)
- for _,key in pairs(key_table) do
- print(key,test_table[key])
- end
- >lua -e "io.stdout:setvbuf 'no'" "test.lua"
- a 3
- b 2
- c 4
- d 1
四、table.concat()
mytable={"a","b","c","d"}
1) table.concat(mytable) 或者 table.concat(mytable,nil) --abcd
2) table.concat(mytable,"%",2) --b%c%d :从第二个位置开始,以%来分隔连接
3) table.concat(mytable,"%",2,3) --b%c :从第二个位置开始,第三个位置结束,以%来分隔
4)a={10,20,30,40}
table.concat(a,"%",2,4)
for k,v in pairs(a) do
print(v)
end --仍然输出10,20,30,40,可见table.concat不会改变原始的table结构
- table库
- table库
- <内核类库>Table
- 文件读写、table库
- Lua5.1 table库
- lua table库
- Lua table库
- lua中的table库
- lua table 库介绍
- lua table库
- Lua table库整理
- 第十九课 table库
- TABLE
- table
- table
- table
- Table
- table
- 前端问题(5)
- 【Python】学习笔记——-2.3、使用List和Tuple
- Android 如何退出整个应用程序?
- Fragment涉及的add、remove和replace方法和回退栈的关系详解
- 基础知识巩固,JavaScript数组复习
- table库
- 蓝桥杯 历届试题 分糖果 水模拟
- fastclick.js提升移动端点击响应速度
- PCI驱动编程
- 【Python】学习笔记——-2.4、条件判断
- JavaScript localeCompare() 方法
- 71道经典Android面试题,涵盖了所有android知识点,值得学习和思考
- Java容器(三):LinkedList源码分析
- c——显式类型转换