关于ipairs()和pairs(),Lua官方手册是这样说明的:
pairs (t)
If t has a metamethod __pairs, calls it with t as argument and returns the first three results from the call.
Otherwise, returns three values: the next function, the table t, and nil, so that the construction
` for k,v in pairs(t) do body end`
will iterate over all key–value pairs of table t.
See function next for the caveats of modifying the table during its traversal.
ipairs (t)
If t has a metamethod __ipairs, calls it with t as argument and returns the first three results from the call.
Otherwise, returns three values: an iterator function, the table t, and 0, so that the construction
` for i,v in ipairs(t) do body end`
will iterate over the pairs (1,t[1]), (2,t[2]), …, up to the first integer key absent from the table.
根据官方手册的描述,pairs会遍历表中所有的key-value值,而pairs会根据key的数值从1开始加1递增遍历对应的table[i]值,直到出现第一个不是按1递增的数值时候退出。
. . .