Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 从数组()中间删除一个条目的最快方法_Actionscript 3_Apache Flex_Flex3 - Fatal编程技术网

Actionscript 3 从数组()中间删除一个条目的最快方法

Actionscript 3 从数组()中间删除一个条目的最快方法,actionscript-3,apache-flex,flex3,Actionscript 3,Apache Flex,Flex3,从数组()中间删除一个特定项的最快方法是什么 数组是一个有字符串的大数组 我不想只将数组[5]=null,相反,数组大小应该减少1,数组[5]应该包含数组[6]等内容。如果您不关心数组中项目的顺序(但只希望它缩短1),您可以将数组的最后一个元素复制到要删除的索引中,然后弹出最后一个元素 array[index] = array[array.length-1]; array.pop(); 我想这是更快的,CPU时间方面的,如果你能逃脱重新排序阵列 编辑:你应该为你的具体案例设定基准;我最近做了这

从数组()中间删除一个特定项的最快方法是什么

数组是一个有字符串的大数组


我不想只将数组[5]=null,相反,数组大小应该减少1,数组[5]应该包含数组[6]等内容。

如果您不关心数组中项目的顺序(但只希望它缩短1),您可以将数组的最后一个元素复制到要删除的索引中,然后弹出最后一个元素

array[index] = array[array.length-1];
array.pop();
我想这是更快的,CPU时间方面的,如果你能逃脱重新排序阵列


编辑:你应该为你的具体案例设定基准;我最近做了这件事,只是拼接更快。(可能是因为Chrome实际上并没有将数组存储为一个连续的缓冲区。)

没有任何基准来支持这一点,但可以假设本机方法是最快的

因此,要删除索引5中的条目:

array.splice(5, 1);
“向数组中添加元素并从中删除元素”:


根据您的情况,如果您想对性能进行优先级排序,可以考虑使用字典而不是数组。

var dict:Dictionary = new Dictionary();

// The following value/key set should be customized so you can 
// get use of them in your specific case.

dict[item1] = item1;
dict[item2] = item2;

...

delete dict[item1];

我测试了Array.prototype.splice(),发现在大型阵列上速度非常慢

删除元素的一种更快的方法是将要保留的元素复制到新数组中,同时跳过要删除的元素。复制完成后,只需用新数组覆盖旧数组

在我的测试中,我从一个包含100.000项的数组中删除了所有其他元素。测试将Array.prototype.splice()与其他方法进行了比较。结果如下:

855 ms = splice
  7 ms = manual copying without preserving the original array
 14 ms = manual copying with preserving the original array
下面是最后一种方法的代码:

var arrB = [],
    i=varA.length,
    j=0;

// copy even items to a new array
while(i > 0) {
    i-=2; // skip two elements
    arrB[j++] = arrA[i];
}

// clear the old array
arrA.splice(0, arrA.length);

// copy values back to the old array
// array is preserved (references to the array don't need to be updated)
arrA.push.apply(arrA, arrB);
可以在JSFIDLE上找到正在运行的测试:

如果只需要删除几个项目,结果会有很大的不同——在这种情况下,Array.prototype.splice()会更快(尽管差异不是很大)!只有在需要多次调用splice()时,才值得实现自定义算法。 第二个测试中,要移除的元素数量有限,可在此处找到:

剪接()确实是行进的方式,但是请记住,如果Flash将“向上移动”,则在大数组中移除中间的东西会很慢。要记住的是,在运行过程中删除一个数组中间的东西会造成严重破坏,除非你在向后运行。对于那些关心特定条目被删除的人来说,记住<代码>剪接< /代码>返回一个数组,所以你需要<代码>数组。剪接(5, 1)[0 ];<这真是太聪明了。比splice:+1快得离谱,一行代码怎么样:array[index]=array.pop()或者甚至array[index]=array[array.length--1]@copy,是的,正确,我后来看到了,但是在有多行代码的情况下,它是一行代码lements@copy这里有一个正确的:(array.length>1)&&(array[index]=array.pop())| | array.pop()@山姆:是的,可能永远都是这样。(哦,我当时觉得很聪明的事情…)
var arrB = [],
    i=varA.length,
    j=0;

// copy even items to a new array
while(i > 0) {
    i-=2; // skip two elements
    arrB[j++] = arrA[i];
}

// clear the old array
arrA.splice(0, arrA.length);

// copy values back to the old array
// array is preserved (references to the array don't need to be updated)
arrA.push.apply(arrA, arrB);