在ActionScript 3和Flash Player 10中使用Vectors
2013-05-31 09:33 小鱼豆豆 阅读(145) 评论(0) 收藏 举报最近想看看flashplayer10新增的Vector类到底是个什么东西,在百度搜到了千篇一律的文章之后,决定打开google搜索国外文章。下面这篇文章是我在Mike Chambers中找到的,在此翻译过来分享给大家。如要转载,请注明出处www.p-boy.cn,谢谢。
One of the new ActionScript features included in the Flash Player 10 Public Beta is the inclusion of a Vector class. Essentially, the Vector class is a typed Array, and in addition to ensuring your collection is type safe, can also provide (sometimes significant) performance improvements over using an Array.
一个ActionScript的新特性是在Flash Player 10 Public Beta中包含了Vector类。基本上,这个Vector类的数据类型是数组,除了确保你收集的是安全类型之外,还提供了优于数组的性能提升(有时是很明显的)。
Using the Vector class is pretty simple, and very similar to using an Array. In fact, the Vector class contains all of the same methods as the Array class. The main difference is how you instantiate it.
使用Vector非常的简单,和使用数组非常相似。事实上,Vector类包含了数组里所有的方法。主要的不同之处在于你怎样去实例化它。
For example, here is how you instantiate an Array:
例如,这里告诉你如何实例化一个数组:
var a:Array = new Array(); //or var b:Array = [];
Here is an example of instantiating a Vector that contains int types:
这里是一个包含有int类型的Vector实例化的例子:
//var VARIABLENAME:Vector.<vectortype> = new Vector.<vectortype>(); var vector:Vector.<int> = new Vector.<int>();
Just as in an Array, you can initialize the Vector length to a specific size, by passing the length into the constructor:
就像数组一样,你可以对Vector的具体大小来实例化它的长度。通过长度来构造:
var size:int = 7; var vector:Vector.<int> = new Vector.<int>(size);
However, the Vector has an additional constructor argument, which is a Boolean value that specifies whether the Vector size is fixed (true) or can be changed (false). The default value is false, and the property can be changed with the fixed property:
然而,Vector还有一个额外的变量,一个可以具体控制Vector长度是否可以改变的布尔值。默认是flash,这一特性可以由fixed属性去改变它。
var size:int = 7; var fixed:Boolean = true var vector:Vector.<int> = new Vector.<int>(size, fixed); vector.fixed = !fixed;
Keep in mind, that if fixed is set to true, then you cannot call any Vector methods that change the length, such as pop(), push(), shift(), etc…
谨记,一旦fixed属性被设置为true,你就不能用Vector的其他方法来改变它的长度,比如pop(), push(), shift()等等。
Vectors are also type safe, so while with an Array you can store multiple types:
Vectors也是安全类型,而数组可以存储多种数据类型:
var s:String = "I am a string"; var d:Date = new Date(); var n:Number = 1138 var a:Array = new Array(); a[0] = s; a[1] = d; a[2] = n; trace(a[1] is Date); //true
You will get a compile time TypeError with a Vector:
在编译Vector时你会得到TypeError错误:
var s:String = "I am a string"; var d:Date = new Date(); var n:Number = 1138 var v:Vector.<string> = new Vector.<string>; v[0] = s; v[1] = d; v[2] = n; trace(v[1] is Date); //false //Compile time errors: //Implicit coercion of a value of type Date to an unrelated type String. //Implicit coercion of a value of type Number to an unrelated type String.
Other than that, working with a Vector is pretty much the same as working with an Array. The APIs are the same, and you can access items directly via their index.
除了Vector运行起来和Array一样漂亮之外,APIs也是相同的,并且你能够通过索引直接访问它们的项目。
var vector:Vector.<int> = new Vector.<int>();
var rand:Number;
for(var i:int = 0; i < 1000000; i++)
{
rand = (Math.floor(Math.random() * 1000000) as int);
vector.push(rand);
}
trace(vector[7]);
One last thing to keep in mind is that a Vector is basically a dense array. This means that all items in the Vector must have either a value or null.
最后一点需要注意的是Vector是一个密集型数组,这意味着里面所有的项目必须存在值或者null。
For example, with an Array, you can do this:
例如,利用Array,你可以这样做:
var a:Array = new Array(); a[0] = "foo"; a[6] = "bar";
But if you try that with a Vector:
但是如果你尝试用Vector做同样的操作的话:
var v:Vector.<string> = new Vector.<string>(); v[0] = "foo"; v[6] = "bar";
You will get a RangeError at runtime.
在运行时你会得到RangeError。
The fix is to initialize the Vector length:
实例化固定长度的Vector:
var v:Vector.<string> = new Vector.<string>(7); v[0] = "foo"; v[6] = "bar";
Below is an example that shows a difference in performance in looping over a million numbers in a collection. Keep in mind that this is one specific test, and depending on your use case, performance improvements may be greater or smaller.
下面这个例子展示了运行超过一百万次循环的不同之处。记住这只是一个特殊的测试,这取决于你所使用的机器,结果可能会偏大或者偏小。
package
{
import flash.display.Sprite;
public class VectorTest extends Sprite
{
private static const NUM_LOOPS:int = 5;
public function VectorTest()
{
var vector:Vector.<int> = new Vector.<int>();
var array:Array = new Array();
//populate data
var rand:Number;
for(var i:int = 0; i < 1000000; i++)
{
rand = (Math.floor(Math.random() * 1000000) as int);
vector.push(rand);
array.push(rand);
}
var sTime:Number = getMilliseconds();
loopArray(array);
trace("Loop Array Avg (5) : " + ((getMilliseconds() - sTime)/NUM_LOOPS));
sTime = getMilliseconds();
loopVector(vector);
trace("Loop Vector Avg (5) : " + ((getMilliseconds() - sTime)/NUM_LOOPS));
}
private function getMilliseconds():Number
{
return (new Date()).getTime();
}
private function loopArray(a:Array):void
{
var len:Number = a.length;
var n:int;
for(var i:int = 0; i < NUM_LOOPS; i++)
{
for(var k:int = 0; k < len; k++)
{
n = a[k];
}
}
}
private function loopVector(v:Vector.<int>):void
{
var len:Number = v.length;
var n:int;
for(var i:int = 0; i < NUM_LOOPS; i++)
{
for(var k:int = 0; k < len; k++)
{
n = v[k];
}
}
}
}
}
On my machine, I get this output:
哦,我在自己的机器上得到了以下输出结果:
Loop Array Avg (5) : 115.8 Loop Vector Avg (5) : 108.8
(译者注:在我的机器上输出的是
Loop Array Avg (5) : 83.4
Loop Vector Avg (5) : 82.4
浙公网安备 33010602011771号