Flash / Flex / ActionScript/Array/Compare — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:14, 26 мая 2010

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
    }
  }
}