我似乎对向量方法removeAt和insertAt遇到了麻烦,它们都出现在as3的文档中:
“ https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#insertAt()”
但是在Adobe flash CS4中,它们是未定义的,并且会引发错误。 CS4是否没有这些方法,或者说明文件不完善?
我似乎对向量方法removeAt和insertAt遇到了麻烦,它们都出现在as3的文档中:
“ https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#insertAt()”
但是在Adobe flash CS4中,它们是未定义的,并且会引发错误。 CS4是否没有这些方法,或者说明文件不完善?
嗯,除了升级到Flash Player和SDK的最新版本外,还有其他选择。这不是一个更好的选择,不是很优雅,但是更简单。除非您正在运行一些庞大的数据集,否则 VPlus.insertAt(...)实现的性能也不是问题。
实施:
package
{
public class VPlus
{
static public function deleteAt(V:*,index:int):void
{
// The method cuts a portion of Vector and returns it.
// We need just the "cut" part,yet it suffices.
V.splice(index,1);
}
static public function insertAt(V:*,index:int,element:*):void
{
// Fix for from-the-end-and-backwards indexing.
if (index < 0)
{
index = V.length - index;
}
// Add one more element to the end of the given Vector.
V.push(null);
// Shift the Vector's elements from index and on.
for (var i:int = V.length - 2; i >= index; i--)
{
V[i+1] = V[i];
}
// Put the new element to its designated place.
V[index] = element;
}
}
}
用法:
// myVec.deleteAt(10);
VPlus.deleteAt(myVec,10);
// myVec.insertAt(15,"Hello World!");
VPlus.insertAt(myVec,15,"Hello World!");