ActionScript中的vector和array

ActionScript中的vector和array

Array:

The Array class lets you access and manipulate arrays. Array indices are zero-based, which means that the first element in the array is [0], the second element is [1], and so on. To create an Array object, you use the new Array() constructor . Array() can also be invoked as a function. In addition, you can use the array access ([]) operator to initialize an array or access the elements of an array.
You can store a wide variety of data types in an array element, including numbers, strings, objects, and even other arrays. You can create a multidimensional array by creating an indexed array and assigning to each of its elements a different indexed array. Such an array is considered multidimensional because it can be used to represent data in a table.

Array允许你操作数组。Array的下标从0开始,意味着[0]表示第一个元素,[1]表示第二个。你可以使用Array()构造一个Array对象。你可以存储任意类型的数据到数组中,包括

数字,字符串,Objects,甚至其它数组。你可以通过创建数组,然后把其它数组作为它的元素来创建多维数组。

Arrays are sparse arrays, meaning there might be an element at index 0 and another at index 5, but nothing in the index positions between those two elements. In such a case, the elements in positions 1 through 4 are undefined, which indicates the absence of an element, not necessarily the presence of an element with the value undefined.

Arrays是稀疏数组,举例:0和5的位置有元素,但是可以没有元素在1和4的位置。

Array assignment is by reference rather than by value. When you assign one array variable to another array variable, both refer to the same array:

数组赋值是引用赋值而不是值的复制。

Copy
 var oneArray:Array = new Array("a", "b", "c");
 var twoArray:Array = oneArray; // Both array variables refer to the same array.
 twoArray[0] = "z";            
 trace(oneArray);               // Output: z,b,c.
 
Do not use the Array class to create associative arrays (also called hashes), which are data structures that contain named elements instead of numbered elements. To create associative arrays, use the Object class. Although ActionScript permits you to create associative arrays using the Array class, you cannot use any of the Array class methods or properties with associative arrays.

You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array.

不要把Array当作关联数组用。要使用关联数组,请使用Object类。尽管Actionscript允许你把Array当作关联数组用,这样的话你就不能用Array类提供的方法或者属性了。

 

Vector:

The Vector class lets you access and manipulate a vector — an array whose elements all have the same data type. The data type of a Vector's elements is known as the Vector's base type. The base type can be any class, including built in classes and custom classes. The base type is specified when declaring a Vector variable as well as when creating an instance by calling the class constructor.
As with an Array, you can use the array access operator ([]) to set or retrieve the value of a Vector element. Several Vector methods also provide mechanisms for setting and retrieving element values. These include push(), pop(), shift(), unshift(), and others. The properties and methods of a Vector object are similar — in most cases identical — to the properties and methods of an Array. In most cases where you would use an Array in which all the elements have the same data type, a Vector instance is preferable. However, Vector instances are dense arrays, meaning it must have a value (or null) in each index. Array instances don't have this same restriction.

The Vector's base type is specified using postfix type parameter syntax. Type parameter syntax is a sequence consisting of a dot (.), left angle bracket (<), class name, then a right angle bracket (>), as shown in this example:

In the first line of the example, the variable v is declared as a Vector.<String> instance. In other words, it represents a Vector (an array) that can only hold String instances and from which only String instances can be retrieved. The second line constructs an instance of the same Vector type (that is, a Vector whose elements are all String objects) and assigns it to v.

Copy
 var v:Vector.<String>;
 v = new Vector.<String>();
 
A variable declared with the Vector.<T> data type can only store a Vector instance that is constructed with the same base type T. For example, a Vector that's constructed by calling new Vector.<String>() can't be assigned to a variable that's declared with the Vector.<int> data type. The base types must match exactly. For example, the following code doesn't compile because the object's base type isn't the same as the variable's declared base type (even though Sprite is a subclass of DisplayObject):

Copy
 // This code doesn't compile even though Sprite is a DisplayObject subclass
 var v:Vector.<DisplayObject> = new Vector.<Sprite>();
 
To convert a Vector with base type T to a Vector of a superclass of T, use the Vector() global function.

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.

 

ActionScript 3.0 中可用的另一种索引数组类型为 Vector 类。Vector 实例是“指定类型的数组”,这表示 Vector 实例中的所有元素始终具有同一数据类型。

注: 从 Flash Player 10 和 Adobe AIR 1.5 开始提供 Vector 类。

在声明 Vector 变量或实例化 Vector 对象时,要显式指定 Vector 可以包含的对象的数据类型。指定的数据类型称为 Vector 的“基本类型”。在运行时和编译时(在严格模式下),会检查任何设置 Vector 元素的值或从 Vector 检索值的代码。如果要添加或检索的对象的数据类型与 Vector 的基本类型不匹配,则会发生错误。

除数据类型限制之外,Vector 类还具有一些其它限制,从而有别于 Array 类:

  • Vector 是一种密集数组。即使某个 Array 对象在位置 1 到 6 没有值,该对象的索引 0 和 7 处也可以有值。但是,Vector 的每个索引位置都必须有值(或为 null)。

  • Vector 还可以是固定长度。这表示 Vector 包含的元素数不能更改。

  • 对 Vector 的元素的访问需要接受范围检查。绝对不能从大于最后一个元素索引 (length - 1) 的索引中读取值。绝对不能对超过当前最后一个索引一个以上位置的索引设置值(也就是说,只能在现有索引或索引 [length] 处设置值)。

可以使用 type 参数语法指定 Vector 的基本类型。在代码中,类型参数紧跟单词 Vector。它包括一个点 (.),然后是由尖括号 (<>) 括起来的基类名称,如此示例中所示:

vector定义数组及类型的方法var v:Vector.<String>; v = new Vector.<String>();

  • 性能:使用 Vector 实例时的数组元素访问和迭代的速度比使用 Array 实例时的速度要快很多。

  • 类型安全性:在严格模式下,编译器可以识别数据类型错误。这类错误的例子包括将数据类型错误的值分配给 Vector 或从 Vector 中读取值时使用错误的数据类型。在运行时,当向 Vector 对象添加数据或从 Vector 对象读取数据时也会检查数据类型。但请注意,当使用 push() 方法或 unshift() 方法向 Vector 添加值时,在编译时不会检查参数的数据类型。不过在使用这些方法时,仍会在运行时检查值。

  • 可靠性:与 Array 相比,运行时范围检查(或固定长度检查)大大提高了可靠性。

除了有一些限制和优点以外,Vector 类与 Array 类非常相似。Vector 对象的属性和方法与 Array 的属性和方法类似(大多数情况下完全相同)。对于大多数需要使用所有元素都具有相同数据类型的 Array 的情况,Vector 实例更为可取。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值