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

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

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

Compose dynamic properties

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var members:Object = new Object();
        members.c1 = "B";
        members.c2 = "D";
        members.c3 = "V";
             
        for (var i:int = 1; i <= 3; i++) {
            trace(members["c" + i]);
        }
    }
  }
}



Create an associative array by adding properties to object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var members:Object = new Object(  );
        members.s = "F";
        members.c = "G";
        members.t = "S";
        
        trace(members.t);
    }
  }
}



Create a single array in which each element is a string containing both pieces of data, separated by a delimiter such as a colon (:)

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

        var aEmployees:Array = new Array();
        aEmployees.push("A:555-1234");
        aEmployees.push("P:555-4321");
        aEmployees.push("C:555-5678");
        aEmployees.push("H:555-8765");
        
        var aTempEmployeeInfo:Array = null;
        for(var i:Number = 0; i < aEmployees.length; i++) {
          aTempEmployeeInfo = aEmployees[i].split(":");
          trace("Employee:" + aTempEmployeeInfo[0]);
          trace("Phone Number:" + aTempEmployeeInfo[1]);
        }
    }
  }
}
Employee:A
Phone Number:555-1234
Employee:P
Phone Number:555-4321
Employee:C
Phone Number:555-5678
Employee:H
Phone Number:555-8765



Creating an Associative Array

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var members:Object = {s: "F",
                              c: "G",
                              t: "S"};
        trace(members);
    }
  }
}



Multidimensional Arrays

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var row1:Array = [6, 2.99];   // Quantity 6, Price 2.99
        var row2:Array = [4, 9.99];   // Quantity 4, Price 9.99
        var row3:Array = [1, 59.99];  // Quantity 1, Price 59.99
        var spreadsheet:Array = [row1, row2, row3];
        
        for (var i:int = 0; i < spreadsheet.length; i++) {
          trace(spreadsheet[i][0]);
        }
    }
  }
}



Reading Elements of an Associative Array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var members:Object = new Object(  );
        members.s= "F";
        members.c= "G";
        members.t= "S";
             
        for (var sRole:String in members) {
            trace(sRole + ": " + members[sRole]);
        }
    }
  }
}



Storing Complex or Multidimensional Data

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

        var colors:Array = ["red", "blue",    "blue",     "gray"];
        var years:Array  = [1997,     2000,       1985,       1983];
        var makes:Array  = ["H",  "C", "M", "F"];
             
        for (var i:int = 0; i < makes.length; i++) {
            trace("A " + colors[i] + " " + 
                         years[i] + " " + 
                         makes[i]);
        }
    }
  }
}
A red 1997 H
A blue 2000 C
A blue 1985 M
A gray 1983 F



The idea behind parallel arrays is to create two (or more) arrays in which the elements with the same indices are related.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aEmployeeNames:Array = ["A", "P", "C", "H"];
        var aEmployeePhone:Array = ["555-1234", "555-4321", "555-5678", "555-8765"];
        for(var i:Number = 0; i < aEmployeeNames.length; i++) {
          trace("Employee:" + aEmployeeNames[i]);
          trace("Phone Number:" + aEmployeePhone[i]);
        }
    }
  }
}
Employee:A
Phone Number:555-1234
Employee:P
Phone Number:555-4321
Employee:C
Phone Number:555-5678
Employee:H
Phone Number:555-8765



Two ways to add properties to dynamic object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var members:Object = new Object(  );
        members["c"] = "R";
        trace(members.c);
        members.c = "a";
        trace(members["c"]);
    }
  }
}



Use nested for loop to deal with multiple sets of data

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var cars:Array = new Array();
        cars.push(["maroon", 1997, "H"]);
        cars.push(["beige", 2000, "Chrysler"]);
        cars.push(["blue", 1985, "Mercedes"]);
        cars.push(["gray", 1983, "Fiat"]);
        
        for (var i:int = 0; i < cars.length; i++) {
            for (var j:int = 0; j < cars[i].length; j++) {
                trace("Element [" + i + "][" + j + "] contains: " + 
                      cars[i][j]);
            }
        }
    }
  }
}
Element [0][0] contains: maroon
Element [0][1] contains: 1997
Element [0][2] contains: H
Element [1][0] contains: beige
Element [1][1] contains: 2000
Element [1][2] contains: Chrysler
Element [2][0] contains: blue
Element [2][1] contains: 1985
Element [2][2] contains: Mercedes
Element [3][0] contains: gray
Element [3][1] contains: 1983
Element [3][2] contains: Fiat



With multidimensional arrays, concat( ) or slice( ) only duplicates the top level of the array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var coordinates:Array = new Array(  );
        coordinates.push([0,1,2,3]);
        coordinates.push([4,5,6,7]);
        coordinates.push([8,9,10,11]);
        coordinates.push([12,13,14,15]);
        
        var coordinatesDuplicate:Array = coordinates.concat(  );
        
        coordinatesDuplicate[0][0] = 20;
        trace(coordinates[0][0]);  // Displays: 20
        
        coordinatesDuplicate[1] = [21,22,23,24];
        trace(coordinates[1]);  // Displays: 4,5,6,7
        
    }
  }
}
20
4,5,6,7



Working with Multidimensional Arrays

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

        var aEmployees:Array = new Array();
        aEmployees.push(["A", "555-1234"]);
        aEmployees.push(["P", "555-4321"]);
        aEmployees.push(["C", "555-5678"]);
        aEmployees.push(["H", "555-8765"]);
        for(var i:Number = 0; i < aEmployees.length; i++) {
          trace("Employee:" + aEmployees[i][0]);
          trace("Phone Number:" + aEmployees[i][1]);
        }
    }
  }
}
Employee:A
Phone Number:555-1234
Employee:P
Phone Number:555-4321
Employee:C
Phone Number:555-5678
Employee:H
Phone Number:555-8765



Working with multiple sets of data: create a multidimensional array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var cars:Array = new Array();
        cars.push(["maroon", 1997, "H"]);
        cars.push(["beige", 2000, "Chrysler"]);
        cars.push(["blue", 1985, "Mercedes"]);
        cars.push(["gray", 1983, "Fiat"]);
             
        for (var i:int = 0; i < cars.length; i++) {
            trace("A " + cars[i][0] + " " + 
                         cars[i][1] + " " + 
                         cars[i][2]);
        }
    }
  }
}
A maroon 1997 H
A beige 2000 Chrysler
A blue 1985 Mercedes
A gray 1983 Fiat



Working with Single-Dimension Arrays

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var aLetters:Array = ["a", "b", "c"];
        var aNoLetters:Array = new Array();
        var aMoreLetters:Array = new Array("d", "e", "f");
    }
  }
}



You can specify the properties of the object in any order you like

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var cars:Array = new Array();
        
        cars.push({make: "H",    year: 1997, color: "maroon"});
        cars.push({make: "Chrysler", year: 2000, color: "beige"});
        cars.push({make: "Mercedes", year: 1985, color: "blue"});
        cars.push({make: "Fiat",     year: 1983, color: "gray"});
             
        for (var i:int = 0; i < cars.length; i++) {
            trace("A " + cars[i].color + " " + 
                         cars[i].year + " " + 
                         cars[i].make);
        }
    }
  }
}
A maroon 1997 H
A beige 2000 Chrysler
A blue 1985 Mercedes
A gray 1983 Fiat