gpt4 book ai didi

apache-flex - 从 Actionscript 3 中的数组中删除元素的更有效方法

转载 作者:行者123 更新时间:2023-12-04 03:04:31 25 4
gpt4 key购买 nike

我有一个对象数组。每个对象都有一个名为 name 的属性。我想从数组中有效地删除具有特定名称的对象。这是最好的方法吗?

  private function RemoveSpoke(Name:String):void {
var Temp:Array=new Array;
for each (var S:Object in Spokes) {
if (S.Name!=Name) {
Temp.push(S);
}
}
Spokes=Temp;
}

最佳答案

如果您愿意在查找表上花费一些内存,这将非常快:

private function remove( data:Array, objectTable:Object, name:String):void {
var index:int = data.indexOf( objectTable[name] );
objectTable[name] = null;
data.splice( index, 1 );
}

对此的测试如下所示:
private function test():void{

var lookup:Object = {};
var Spokes:Array = [];
for ( var i:int = 0; i < 1000; i++ )
{
var obj:Object = { name: (Math.random()*0xffffff).toString(16), someOtherProperty:"blah" };
if ( lookup[ obj.name ] == null )
{
lookup[ obj.name ] = obj;
Spokes.push( obj );
}
}

var t:int = getTimer();
for ( var i:int = 0; i < 500; i++ )
{
var test:Object = Spokes[int(Math.random()*Spokes.length)];
remove(Spokes,lookup,test.name)
}
trace( getTimer() - t );

}

关于apache-flex - 从 Actionscript 3 中的数组中删除元素的更有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2915927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com