正向循环删除元素
删除所有匹配的元素
function RemoveMatchItems(list, matchFunc) local removeCt = 0 local ct = #list local i = 1 while i <= ct do if matchFunc(list[i]) then table.remove(list, i) i = i - 1 ct = ct - 1 removeCt = removeCt + 1 end i = i + 1 end return removeCt end
测试代码
local list = { "a", "b", "c" } local ct = RemoveMatchItems(list, function(item) return "b" == item end) print(ct, list[1], list[2]) --1 a c