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

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

Версия 09:19, 26 мая 2010

A list of parameters, each of which is a new value to insert into a new element in the array

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        // Create a new array with zero elements.
        var aEmployees:Array = new Array("A", "P", "C","H");
        
        trace(aEmployees);
    }
  }
}



Arrays in ActionScript can contain any type of object:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var time:Date = new Date();
        var currentTemp:Number = 56;
        var currentConditions:String = "Light Showers";
        var weatherReport:Array = new Array(time, currentTemp, currentConditions);
        trace(weatherReport);
    }
  }
}



A single parameter specifying the number of elements creates a new array with the specified number of elements.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        // Create a new array with four elements.
        var aEmployees:Array = new Array(4);
        
        trace(aEmployees);
    }
  }
}



Converting Arrays to Strings: by default, arrays display a comma-separated list of values.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var names:Array = ["Jenene", "Josh", "Jac"];
        trace(names); // Displays: Jenene,Josh,Jac
    }
  }
}



Creating Arrays: No parameters constructor creates a new array with zero elements.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        // Create a new array with zero elements.
        var aEmployees:Array = new Array();
        trace(aEmployees);
    }
  }
}



Delete elements and insert new elements at the same time

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var letters:Array = ["a", "b", "c", "d"];
             
        letters.splice(1, 2, "r", "s", "t");
             
        for (var i:int = 0; i < letters.length; i++) {
            trace(letters[i]);
        }
    }
  }
}



Inserting Elements in the Middle of an Array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var letters:Array = ["a", "b", "c", "d"];
             
        letters.splice(1, 0, "r", "s", "t");
             
        for (var i:int = 0; i < letters.length; i++) {
            trace(letters[i]);
        }
    }
  }
}
a
r
s
t
b
c
d



Is element in an array undefined

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var numbers:Array = new Array(4, 10);
        numbers[4] = 1;
        trace(numbers);  
        for(var i:int = 0; i < numbers.length; i++) {
          if(numbers[i] == undefined) {
            numbers.splice(i, 1);
            i--;
          }
        }
        trace(numbers);  
    }
  }
}
// 4,10,,,1
// 4,10,1



Set an element with an index that doesn"t exist

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var letters:Array = ["a", "b", "c"];
        letters[5] = "f";
        
        trace(letters);
    }
  }
}



Use array literal notation as an alternative to the first variation of the constructor.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = [];
        
        trace(aEmployees);
    }
  }
}



Within the square brackets you can provide a list of elements to add to the array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployees:Array = ["A", "P", "C", "H"];
        
        trace(aEmployees);
    }
  }
}