lua table怎么用二维数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lua table怎么用二维数组相关的知识,希望对你有一定的参考价值。
参考技术A 可以用for来遍历:[cpp] view plaincopy
do
table_week =
"w",
"e",
"r",
"t",
"y",
"u",
"i",
for i = 1, #table_week do
print(table_week[i])
end
end
#后面接一个数组或者tabe来遍历它,i是该table或者数组的起始下标。本回答被提问者和网友采纳
Lua用table模拟数组
1 local array = {}; 2 local row1 = {1, 3, 5, 7, 9} 3 local row2 = {2, 4, 6, 8, 10} 4 local row3 = {"I", "love", "lua"} 5 array[1] = row1; 6 array[2] = row2; 7 array[3] = row3 8 print("length = ", #array); 9 for i = 1, #array do 10 for j = 1, #array[i] do 11 print(array[i][j]) 12 end 13 end
1 function Create2DArray(row, col) 2 local array2D = {} 3 for i = 1, row do 4 array2D[i] = {} 5 for j = 1, col do 6 array2D[i][j] = 0 7 end 8 end 9 return array2D 10 end
1 function Print2DArray(array) 2 local str = "" 3 for i = 1, #array do 4 for j = 1, #array[i] do 5 str = table.concat(array[i], " ") 6 end 7 print(str) 8 end 9 end
以上是关于lua table怎么用二维数组的主要内容,如果未能解决你的问题,请参考以下文章