Table库函数
note 目录
- 取下标 tableindex
- 插入 tableinsert
- 删除 tableremove
- 插入 tablegetn
- 连接 tablecontat
- 排序tablesort
1:取下标 table[index]
在初始化一个表的table
时候的时候,若不显示的对表的键值进行赋值的话,默认就是数组的形式保存。默认数字作为下标从1开始。
local a = {"a","b","c","d","e"}
for i = 1, 5 do
print("a[i]")
end
输出结果:
a
b
c
d
e
f
更复杂一点的例子
local color={first="red", "blue", third="green", "yellow"}
print(color["first"]) --> output: red
print(color[1]) --> output: blue
print(color["third"]) --> output: green
print(color[2]) --> output: yellow
print(color[3]) --> output: nil
2:插入 table.insert
table.insert(tab,pos)
函数table.insert用于将一个元素插入到一个数组的指定位置。它会移动后续的元素以空出空间。
local a = {10,20,30}
table.insert(a,1,15)
for index,value in ipairs(a)
print("currentIndex:" .. index .. " " .. "currentValue:" .. value)
end
output:
currentIndex:1 currentValue:15
currentIndex:2 currentValue:10
currentIndex:3 currentValue:20
currentIndex:4 currentValue:30
若没有指定位置则默认添加到末尾
local a = {10,20,30}
table.insert(a,40)
for index,value in ipairs(a)
print("currentIndex:" .. index .. " " .. "currentValue:" .. value)
output:
currentIndex:1 currentValue:10
currentIndex:2 currentValue:20
currentIndex:3 currentValue:30
currentIndex:4 currentValue:40
3:删除 table.remove
table.remove(tab,pos)
table.remove
会删除并返回数组指定位置上pos
的元素,并将该位置之后的所有的元数的索引减一。
如果在调用table.remove
这个函数是不指定删除元素的位置,他就会删除数组的最后一个元素。
local a = {10,20,30,40}
--指定位置删除
table.remove(a,1)
print(a)
output:
20
30
40
--不指定位置删除
local b = {1,2,3,4}
table.remove(b)
print(b)
output:
1
2
3
4:插入 table.getn
table.getn(tab)
table.getn
取长度操作,与#操作符是意一样的。字符串的长度就是它的字节数
对于常规的数组,里面从 1 到 n 放着一些非空的值的时候, 它的长度就精确的为 n,即最后一个值的下标。 如果数组有一个“空洞” (就是说,nil 值被夹在非空值之间), 那么 #t 可能是指向任何一个是 nil 值的前一个位置的下标 (就是说,任何一个nil 值都有可能被当成数组的结束)。这也就说明对于有“空洞”的情况,table 的长度存在一定的 不可确定性。
local tblTest1 = { 1, a = 2, 3 }
print("Test1 " .. table.getn(tblTest1))
local tblTest2 = { 1, nil }
print("Test2 " .. table.getn(tblTest2))
local tblTest3 = { 1, nil, 2 }
print("Test3 " .. table.getn(tblTest3))
local tblTest4 = { 1, nil, 2, nil }
print("Test4 " .. table.getn(tblTest4))
local tblTest5 = { 1, nil, 2, nil, 3, nil }
print("Test5 " .. table.getn(tblTest5))
local tblTest6 = { 1, nil, 2, nil, 3, nil, 4, nil }
print("Test6 " .. table.getn(tblTest6))
output:
Test1 2
Test2 1
Test3 1
Test4 1
Test5 1
Test6 1
5:连接 table.contat
table.contat(tab,insertChat = '',start = 1,end = #table)
它接受一个字符string
或number
类型的数组,并返回数组连接后的结果。
(1)insertChat是一个可选参数,用于指定插到字符串之间的分隔符。
(2)start 可选参数,数组起始连接的位置。
(3)end 可选参数,数组结束连接的位置。
start默认是1,end默认是#tab数组的长度。
若start > end,则返回一个空的字符串。
local a = {1,2,3,4,"hello","wrold"}
local str1 = table.contat(a)
print("str1:" .. str1)
local str2 = table.contat(a,"|")
print("str2:" .. str2)
local str3 = table.contat(a," ", 5,2)
print("str3" .. str3)
local str4 = table,contat(a, " " , 2,5)
print("str4" .. str4)
output:
str1:1234hellowrold
str2:1|2|3|4|hello|wrold
str3:
str4:2 3 4 hello
6:排序table.sort
(1)table.sort(tab)函数对给定的table进行升序排序.
tbl = {"alpha", "beta", "gamma", "delta"}
table.sort(tbl)
print(table.concat(tbl, ", "))
output:
alpha, beta, delta, gamma
(2)按照给定的比较函数,从tab[1]到tab[n]进行排序,比较函数compare
有2个参数compare(a,b)
如果想第一个参数排在第二个参数前面,返回true,否则返回false,默认是从小到大的排序。
local tab = {1,5,2,7,4,3,6}
local funciton compare(a,b)
return a > b
end
print("before sort:")
for i = 1 , #tab do
print(tab[i])
end
print("after sort:")
table.sort(tab,compare)
for i = 1 , #tab do
print(tab[i])
end
output:
before sort:
1
5
2
7
4
3
6
after sort:
7
6
5
4
3
2
1