在一个循环内删除多个元素
?
package { import flash.display.Sprite; public class TestArrayDelete extends Sprite { public function TestArrayDelete() { var needDeleteNum:int = 0; var allElementVector:Vector.<TestElement> = new Vector.<TestElement>(); for (var i:int = 0; i < 100; i++) { var newE:TestElement = new TestElement(); newE.elementIndex = i; newE.isDelete = Random.boolean; if (newE.isDelete) { needDeleteNum++; } allElementVector.push(newE); } trace("--->Need DeleteNum : " + needDeleteNum); trace("================"); var totalDeleteNum:int = 0; var totalLoopNum:int = 0; //反向遍历删除数组 for (var j:int = allElementVector.length - 1; j >= 0; j--) { var delE:TestElement = allElementVector[j]; if (delE.isDelete) { allElementVector.splice(j, 1); totalDeleteNum++; } totalLoopNum++; } trace("--->Total DeleteNum : " + totalDeleteNum); trace("--->Total LoopNum : " + totalLoopNum); } } }