AS 3-1サイクルで複数の要素を削除

1290 ワード

1つのループ内で複数の要素を削除
 
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);
		}
	}
}