Lua丨数组

版权声明:欢迎转载,转载请注明出处 https://blog.csdn.net/weixin_38239050/article/details/82496230

一维数组

array={"lua","skode"}

--Lua中顺序是从1开始,而非C#的0
for i=1,2 do
	print(array[i])
end

--Lua以键值对形式存储数据,所以索引可为负数
array={}
for i=-2,2 do
	array[i]=i*3
	print(array[i])
end




>lua -e "io.stdout:setvbuf 'no'" "table.lua" 
lua
skode
-6
-3
0
3
6
>Exit code: 0

多维数组

--二维数组
array={{1.1,1.2},{2.1,2.2},{3.1,3.2},{4.1,4.2}}   --4x2
print(array[1][1])

--遍历数组
for i=1,4 do
	for j=1,2 do
		print(array[i][j])
	end
end

--创建二维数组
array2={}
for i=1,3 do
	array2[i]={}
	for j=1,2 do
		array2[i][j]=i*j
	end
end





>lua -e "io.stdout:setvbuf 'no'" "table.lua" 
1.1
1.1
1.2
2.1
2.2
3.1
3.2
4.1
4.2
>Exit code: 0

猜你喜欢

转载自blog.csdn.net/weixin_38239050/article/details/82496230
今日推荐