Document directory
- Basic Requirements
- Create a vector instance
- Preset vector Length
- Create a vector with dynamically Adjustable Length
- Add a value to a vector object
Use Vector Programming
OneArray)It is like a container that organizes a set of variables together. A single array can contain many different values. You can store and obtain a single value in an array (that is, an array ).Elements)). You can also group the values by directly operating on the array variables. The most common isOrdered array. You can obtain the value from an index number. Action script3 includes two classes used to index arrays.
- Array class:An index array that can contain various value types. Its volume can even allow you to mix different value types in the same number of groups.
- Vector class:It is also an index array. The elements must be instances of the same class. The vector class is applicable to Flash Player 10 and later versions.
If you need to store a series of values with the same data type, the vector class has some advantages over the array class. First, because all elements of a vector must have the same data type, The ActionScript compiler can perform type check during code compilation. Any code that tries to add or restore the error type value will be processed as a compilation error. The data type is also checked during runtime. Therefore, if the data type cannot be checked during compilation, it will still be checked and the Data Type restriction is valid. In addition to the advantages of type check, code using the Vector class runs faster than the same Code created using array-this is an important advantage.
This Quick Start describes how to create a Vector class. For more information about how to use the array class, see the Quick Start of flash to use array programming.
The following section describes how to use a vector object. The following section describes how to use a vector object to complete a general task: Create a vector instance, add a value to a vector object, and change the value to a vector object, is the value Classification of a vector object.
Basic Requirements
To implement the instance in this quick start, you need the following instance file:
Sample file
- Programming_with_vector.zip
The archive file in this example contains the following files:
- Sortvectorofnumber. fla:A flash file shows how to sort numeric values in a vector. It is described in the section "Sort values in a vector" in this article.
- Sortvectorofobject. fla:An example shows how to sort common objects in a vector. This section describes how to sort values in a vector.
- Person.:The source code of the person class, which is referenced in sortvectorofobject. Fla.
To test each application, open the FLA file and select "control> test video ".
Prerequisites
We recommend that you have general experience in actionscript3 programming.
Create a vector instance
A vector object is an index array that only stores certain values. A specific class of a vector object is the basic type of a vector. A vector object is created when the Vector class constructor is called. As you may have guessed, the basic class specified for the vector variable must match the basic type specified when calling the constructor.
When creating a vector instance, to specify its basic type, you need to add additional information to the Vector class name and use a syntax format called a type parameter. For example, the following statement declaresmyVector
The vector variable inmyVector
The vector can only contain values of the string type.
var myVector:Vector.<String>;
When you call the vector constructor to actually create a vector object, you can use the same syntax:
myVector = new Vector.<String>();
As a ruleVector
This class name appears in your code, and you always need to use a type parameter for it.
Of course, you can integrate variable declarations and constructor instances into a single line of code.
var myVector:Vector.<String> = new Vector.<String>();
Although the examples given in this quick start are relatively simple, for example, creating string and number as the base type of the vector, in fact, the base type of the Vector class can be any class. This also includes your custom class. For example, assume that your code defines a class named myclass. In this case, the following code is valid. It creates a vector object and its elements must be myclass instances:
var v:Vector.<MyClass> = new Vector.<MyClass>();
Preset vector Length
The vector constructor has two optional parameters that allow you to specify the number of your vector instances. The first parameter islength
Parameters. By default, when a vector is created, it is null (it has 0 elements ). However, if you pass a valuelength
Parameter to specify the number of elements. The vector instance is created:
var myVector:Vector.<String> = new Vector.<String>(7); // myVector is created with 7 elements
Pre-determine the vector size is more efficient than creating an element at a time. Therefore, if you know in advance how many elements a vector contains, providelength
The parameter value is good. If the base type of a vector is a Boolean or numeric type (number, Int, uint), each element is assigned the default value of this data type (the default value of Boolean ).false
, The Data Type defaults to 0 ). Otherwise, the initial value of each element isnull
.
Create a vector with dynamically Adjustable Length
Another feature of vector objects is that they can adjust the length, which means that you can change the value but cannot change the total number by adding or deleting elements. By default, a vector instance does not allow dynamic length modification. To create a variable-length vector, set the second parameter of the vector Constructor (fixed
Parameter)true
:
var myVector:Vector.<String> = new Vector.<String>(7, true); // myVector's length is fixed to 7 elements
Note:You can modifyfixed
Attribute to set whether the length of the vector is variable.
myVector.fixed = false; // myVector's length is no longer fixed
Add a value to a vector object
In addition to an additional restriction, adding a value to a vector object is similar to adding a value to an array object. Each element of a vector object must have a value (ornull
). In other words, you cannot add a value at the position where the serial number is 4, unless 0-3 of the vector already has a value. In practice, this means that to add a new element to the vector, you must add it at a position equal to the serial number of the vector object (because the first element of the vector is 0length
The attribute value is usually larger than the serial number of the last element of the vector ). The following code demonstrates this technique:
var names:Vector.<String> = new Vector.<String>(); // ... assume some elements are added ... // Add an element to the end of the Vectornames[names.length] = "Bob";
In addition to the array accessors ([]
) Operation to add a value to a vector object. You can also usepush()
Orunshift()
Method to add elements to the vector. Just like the array class,push()
Method to create a new element at the end of the last element of the vector,unshift()
Method to create a new element in the position where the vector serial number is 0 (and all existing elements are moved to the position with the highest value ):
names.push("Harold"); // adds the value "Harold" to the end of the Vectornames.unshift("Fred"); // adds the value "Fred" to the start of the Vector
The additional advantage of these methods is that you can pass multiple values as parameters to the method, and all values will be added to the vector object at one time. Then, this elasticity will also lead to a consequence. When you usepush()
Orunshift()
When adding a value to a vector object, the compiler cannot check whether the data type matches. Because, any usepush()
Orunshift()
The code that adds an error type value to the vector object will be discovered only when it is known to run the code.
Restore a value from a vector object
The recovery value from a vector object is exactly the same as that from an array object. To return element values from a specific sequence number, you need to use the array accessors ([]
) Operation to specify the sequence number of the elements you need:
var name1:String = names[0];
Use the array accessors ([]
), But does not remove it from the vector object. To restore the value and remove it from the vector object, usepop()
Method (it will remove the last element) orshift()
(It removes 0th elements and removes one of them ):
var finalName:String = names.pop(); // removes the last value from the Vectorvar firstName:String = names.shift(); // removes the first value from the Vector
Sorts values in a vector.
Most methods for using a vector object are the same as those for using an array object. One method you need to know is that the elements in the vector object are sorted. The vector class has only one way to sort values:sort()
Method.sort()
The method does not change the original vector object. Instead, it returns a new vector with the same base type containing sorted values.
When you use the Vector classsort()
Method, it does not have the default sorting behavior, even if it is a basic data type such as number or string. Because of this, you can usesort()
You must specify a custom sorting function to define the sorting logic. For example, in the following code, a number-type vector object usessort()
Method. In this case, the Code demonstrates basic data sorting; small numbers are placed before large numbers (ascending ). NamesortNumbers()
The function defines the sorting behavior, which is passed as a parametersort()
Method call. Flash Player callssortNumber()
Function, the two values to be compared are passed to the function, and the result determines the final sorting order:
var numberVector:Vector.<Number> = new Vector.<Number>();numberVector.push(2, 17, 3.6, 4.4, 29, -34, 0.09235); trace(numberVector); // output: 2,17,3.6,4.4,29,-34,0.09235 var sortedVector:Vector.<Number> = numberVector.sort(sortNumbers); trace(sortedVector); // output: -34,0.09235,2,3.6,4.4,17,29 function sortNumbers(x:Number, y:Number):Number{ if (x < y) { return -1; } else if (x > y) { return 1; } else { return 0; }}
You can define an ordering function for any data type. For example, the following code sorts the person object of a vector by the last name, and then first name (it assumes that a person class hasfirstName
AndlastName
Attribute ):
var personVector:Vector.<Person> = new Vector.<Person>();personVector[0] = new Person("Bob", "Smith");personVector[1] = new Person("Harold", "Johnson");personVector[2] = new Person("Barbara", "Smith");personVector[3] = new Person("Arnold", "Anderson");personVector[4] = new Person("Margaret", "Wooster"); // output:[Smith,Bob],[Johnson,Harold],[Smith,Barbara],[Anderson,Arnold],[Wooster,Margaret]trace(personVector); var sortedVector:Vector.<Person> = personVector.sort(sortPeople); // output:[Anderson,Arnold],[Johnson,Harold],[Smith,Barbara],[Smith,Bob],[Wooster,Margaret]trace(sortedVector); function sortPeople(x:Person, y:Person):Number{ // sort by last name var lastNameSort:Number = sortStrings(x.lastName, y.lastName); if (lastNameSort != 0) { return lastNameSort; } else { // if the last names are identical, sort by first name return sortStrings(x.firstName, y.firstName); }} function sortStrings(x:String, y:String):Number{ if (x < y) { return -1; } else if (x > y) { return 1; } else { return 0; }}