b = {} for i=1, 10 do b[i] = i end -- method one for i, v in pairs(b) do print (i, v) end -- method two for i, v in ipairs(b) do print (i, v) end -- method three for i = 1, #b do print (i, b[i]) end -- method four for i = 1, table.maxn(b) do print (i, b[i]) end
lua常用的四种遍历,前两种是泛型遍历,后两种是数值的遍历.