Flash / Flex / ActionScript/Array/Array Index

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Adding New Elements with the length Variable

 

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



Adding Values to an Array: Assigning Values Using Array-Access Notation

 
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());
    }
  }
}



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.

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



Read the data from an array using array-access notation to access one element at a time.

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



Referencing Array Elements

 

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



Setting an Element"s Value

 

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



Sum the array elements

 

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



To add a new element to an existing array at a specific index: assign a value to that element.

 

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



Use variable as the array index

 

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



Use variables or complex expressions to specify an element index

 

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