码迷,mamicode.com
首页 > 编程语言 > 详细

Lua用table模拟数组

时间:2018-12-22 22:08:40      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:int   row   cat   concat   style   create   color   div   func   

 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模拟数组

标签:int   row   cat   concat   style   create   color   div   func   

原文地址:https://www.cnblogs.com/blackteeth/p/10162303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!