您的位置:首页 > 其它

Alternatives to Array in Actionscript3.0

2012-10-05 12:55 405 查看
It is recommended by Adobe authority, that if
Array's functionality is not a must, then use
Vector to replace it.

class someClass
{
private var _topMenuItems:Vector.<PavoTweenMenuItem> = new Vector.<PavoTweenMenuItem>();

//...

private function someMethod():void
{
var $item:PavoTweenMenuItem = new PavoTweenMenuItem();
//...do other stuff
this._topMenuItems.push($item);
}

private function anotherMethod():void
{
for (var $i:int=0; $i<this._topMenuItems.length; $i++)
{
var $item:PavoTweenMenuItem = this._topMenuItems[$i] as PavoTweenMenuItem;
//...do other stuff
}
}

private function yetAnotherMethod():void
{
for each (var $item:PavoTweenMenuItem in this._topMenuItems)
{
//...do something with $item
}
}
}


And how to implement a 2-dimension Vector:

private var _subMenuItems:Vector.<Vector.<PavoTweenSubMenuItem>>;

private function someMethod
{
this._subMenuItems = new Vector.<Vector.<PavoTweenSubMenuItem>>($mun, true);	// specify its length, and fix it

var $subItems:Vector.<PavoTweenSubMenuItem> = new Vector.<PavoTweenSubMenuItem>();

for(...)
{
var $subItem:PavoTweenSubMenuItem = new PavoTweenSubMenuItem();
$subItems.push($subItem);
}

this._subMenuItems[$i] = $subItems;
}


In addition to the data type restriction, the Vector class has other restrictions that distinguish it from the Array class:

A Vector is a dense array. Unlike an Array, which may have values in indices 0 and 7 even if there are no values in positions 1 through 6, a Vector must have a value (or
null
)
in each index.
A Vector can optionally be fixed-length, meaning the number of elements it contains can't change.
Access to a Vector's elements is bounds-checked. You can never read a value from an index greater than the final element (
length - 1
). You can never set a
value with an index more than one beyond the current final index (in other words, you can only set a value at an existing index or at index
[length]
).

As a result of its restrictions, a Vector has three primary benefits over an Array instance whose elements are all instances of a single class:

Performance: array element access and iteration are much faster when using a Vector instance than they are when using an Array.
Type safety: in strict mode the compiler can identify data type errors. Examples of data type errors include assigning a value of the incorrect data type to a Vector or expecting the wrong data type when reading a value from a Vector. Note, however, that
when using the
push()
method or
unshift()
method to add values
to a Vector, the arguments' data types are not checked at compile time. Instead, they are checked at run time.
Reliability: runtime range checking (or fixed-length checking) increases reliability significantly over Arrays.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: