在Lua中,table如何安全的移除元素这点挺重要,因为如果不小心,会没有正确的移除,造成内存泄漏。
引子
比如有些朋友常常这么做,大家看有啥问题
将test表中的偶数移除掉
local test = { 2, 3, 4, 8, 9, 100, 20, 13, 15, 7, 11} for i, v in ipairs( test ) do if v % 2 == 0 then table.remove(test, i) end endfor i, v in ipairs( test ) do print(i .. "====" .. v) end
1====3 2====8 3====9 4====20 5====13 6====15 7====7 8====11 [Finished in 0.0s]
如何做呢?
Let's get started!
local test = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p' } local remove = { a = true, b = true, c = true, e = true, f = true, p = true }local function dump(table) for k, v in pairs( table ) do print(k) print(v) print("*********") end end
方法1 从后往前删除
for i = #test, 1, -1 do if remove[test[i]] then table.remove(test, i) end enddump(test)
方法2 while删除
local i = 1 while i <= #test do if remove[test[i]] then table.remove(test, i) else i = i + 1 end end
方法3 quick中提供的removeItem
function table.removeItem(list, item, removeAll) local rmCount = 0 for i = 1, #list do if list[i - rmCount] == item then table.remove(list, i - rmCount) if removeAll then rmCount = rmCount + 1 else break end end end endfor k, v in pairs( remove ) do table.removeItem(test, k) end
dump(test)