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.
And how to implement a 2-dimension Vector:
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
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 (
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
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
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.
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.
相关文章推荐
- Looking up through Array in Actionscript 3.0
- Bitmap & BitmapData classes in Actionscript 3.0
- Reflection in Actionscript 3.0/Flex 2
- How to Fake a AsyncToken return in ActionScript 3
- Garbage Collection in Actionscript3.0
- Loop Through Object in Actionscript3.0
- Custom Event in ActionScript3.0
- Error message when you try to modify or to delete an alternate access mapping in Windows SharePoint Services 3.0: "An update conflict has occurred, and you must re-try this action"
- root, stage and timeline in ActionScript3.0
- Constructor Overloading in Actionscript 3.0
- Capture Keyboard Event in Actionscript3.0
- The Essential Guide to Flex 2 with ActionScript 3.0
- Create a Drag and Drop Puzzle in ActionScript 3.0
- Detect Chinese character through out a string in Actionscript3.0
- Empty, Unavailable, Invalid Variables Handling in Actionscript 3.0 & PHP 5+
- flash actionscript 3.0 赞
- How to initialize an array in C
- Unicode String to a UTF-8 TypedArray Buffer in JavaScript
- as3corelib Tutorial:How to Use ArrayUtil Class in Flex
- PowerShell Script to Deploy Multiple VM on Azure in Parallel #azure #powershell