数组的逆序,只能用于数组,不能用于哈希表
function reverseTable(tab) local tmp = {} for i = 1, #tab do local key = #tab tmp[i] = table.remove(tab) end return tmp end // 示例 local t = {"one", "two", "three"} for k, v in pairs(t) do print(k, v) end t = reverseTable(t) for k, v in pairs(t) do print(k, v) end
输出:
1 one
2 two
3 three
1 three
2 two
3 one
时间: 2024-10-06 08:09:30