Flash / Flex / ActionScript/Array/Array Index
Версия от 12:19, 26 мая 2010; (обсуждение)
Содержание
- 1 Adding New Elements with the length Variable
- 2 Adding Values to an Array: Assigning Values Using Array-Access Notation
- 3 if you assign a value to an element that has not yet been created, actionscript creates that element, and any other elements with indices between.
- 4 Read the data from an array using array-access notation to access one element at a time.
- 5 Referencing Array Elements
- 6 Setting an Element"s Value
- 7 Sum the array elements
- 8 To add a new element to an existing array at a specific index: assign a value to that element.
- 9 Use variable as the array index
- 10 Use variables or complex expressions to specify an element index
Adding New Elements with the length Variable
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var colors = ["green", "red", "blue"]; colors.length = 50; trace(colors); // green,red,blue,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, } }
}
</source>
Adding Values to an Array: Assigning Values Using Array-Access Notation
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var aEmployees:Array = new Array(4); aEmployees[0] = "A"; aEmployees[1] = "P"; aEmployees[2] = "C"; aEmployees[3] = "H"; trace(aEmployees.toString()); } }
}
</source>
if you assign a value to an element that has not yet been created, actionscript creates that element, and any other elements with indices between.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var aEmployees:Array = new Array(4); aEmployees[0] = "A"; aEmployees[1] = "P"; aEmployees[2] = "C"; aEmployees[3] = "H"; aEmployees[9] = "Ruth"; trace(aEmployees.toString()); //A,P,C,H,,,,,,Ruth } }
}
</source>
Read the data from an array using array-access notation to access one element at a time.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var aEmployees:Array = ["A", "P", "C", "H"]; trace(aEmployees[0]); // Displays: A trace(aEmployees[1]); // Displays: P trace(aEmployees[2]); // Displays: C trace(aEmployees[3]); // Displays: H } }
}
</source>
Referencing Array Elements
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var trees:Array = ["b", "m", "o", "c"]; var firstTree:String = trees[0]; var favoriteTree:String = trees[2]; trace(firstTree); trace(favoriteTree); } }
}
</source>
Setting an Element"s Value
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var cities:Array = ["Toronto", "Montreal", "Vancouver", "Waterloo"]; cities[0] = "London"; cities[3] = "Hamburg"; cities[2] = 293.3; // Notice that the datatype change is not a problem trace(cities); } }
}
</source>
Sum the array elements
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var ages:Array = [12, 4, 90]; var totalAge:Number = ages[0] + ages[1] + ages[2]; // Sum the array trace(totalAge); } }
}
</source>
To add a new element to an existing array at a specific index: assign a value to that element.
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var fruits:Array = ["apples", "oranges", "pears"]; fruits[3] = "tangerines"; fruits[39] = "grapes"; trace(fruits); // Displays: undefined trace(fruits[12]); // Displays: undefined
} }
} apples,oranges,pears,tangerines,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,grapes undefined
</source>
Use variable as the array index
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){ var cities:Array = ["Toronto", "Montreal", "Vancouver", "Waterloo"]; var i:int = 1; cities[i] = "Beijing"; trace(cities); //Toronto,Beijing,Vancouver,Waterloo } }
}
</source>
Use variables or complex expressions to specify an element index
<source lang="java">
package{
import flash.display.Sprite; public class Main extends Sprite{ public function Main(){
var trees:Array = ["b", "m", "o", "c"]; var i = 3; var lastTree:String = trees[i]; var randomTree:String = trees[Math.floor(Math.random( ) * 4)]; trace(lastTree); trace(randomTree); } }
}
</source>