Flash / Flex / ActionScript/Array/Compare — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Содержание
- 1 Array reference
- 2 Create a duplicate copy of an array that is separate from the original using concat( ):
- 3 if two arrays are equivalent yet don"t point to the same spot in memory, an equality operation returns false:
- 4 Loop through each of the elements of the arrays and compare them
- 5 Using an equality operator with two array variables only checks to see if they point to the same spot in memory.
Array reference
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c"];
var newLetters:Array = letters;
trace(letters); // Displays: "a,b,c"
trace(newLetters); // Displays: "a,b,c"
letters = ["d", "e", "f"];
trace(letters); // Displays: "d,e,f"
trace(newLetters); // Displays: "d,e,f" (not "a,b,c")
}
}
}
Create a duplicate copy of an array that is separate from the original using concat( ):
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c"];
var newLetters:Array = letters.concat( );
trace(letters); // Displays: "a,b,c"
trace(newLetters); // Displays: "a,b,c"
letters = ["d", "e", "f"];
trace(letters); // Displays: "d,e,f"
trace(newLetters); // Displays: "a,b,c"
}
}
}
a,b,c
a,b,c
d,e,f
a,b,c
if two arrays are equivalent yet don"t point to the same spot in memory, an equality operation returns false:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["a", "b", "c", "d"];
trace(letters1 == letters2); // Displays: false
}
}
}
Loop through each of the elements of the arrays and compare them
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters1:Array = ["a", "b", "c", "d"];
var letters2:Array = ["a", "b", "c", "d"];
var equivalent:Boolean = true;
for(var i:int = 0; i < letters1.length; i++) {
if(letters1[i] != letters2[i]) {
equivalent = false;
break;
}
}
trace(equivalent); // Displays: true
}
}
}
Using an equality operator with two array variables only checks to see if they point to the same spot in memory.
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var letters:Array = ["a", "b", "c", "d"];
var lettersPointer:Array = letters;
trace(letters == lettersPointer); // Displays: true
}
}
}