as3多维数组

时间:2012-02-13 22:58:42

标签: actionscript-3 multidimensional-array

我试图创建一个多维数组但是我得到一个错误(“TypeError:错误#1010:一个术语未定义且没有属性。”)。

var matriz:Array = new Array();
for(var p:Number = 0; p<2;p++ ){
    for(var q:Number = 0; q<2;q++ ){
        matriz[p][q] = 0;
    }
}

我做错了什么?

提前致谢!

3 个答案:

答案 0 :(得分:3)

您需要在matriz[p]内创建一个数组,然后才能向其中添加数组(或其他任何内容)。

你可以在没有错误的情况下实现你正在尝试的目标:

var matriz:Array = [];

for(var p:Number = 0; p<2; p++)
{
    // Create an array at matriz[p] if undefined.
    if(matriz[p] == undefined) matriz[p] = [];

    for(var q:Number = 0; q<2; q++)
    {
        matriz[p][q] = 0;
    }
}

基本上你试图像这样做:

var object:Object = {};
object.nonexistantProperty.value = 10;

答案 1 :(得分:2)

Marty所说的是正确的,但我更喜欢删除if条件并将代码更改为以下内容:

var matriz:Array = [];

for(var p:Number = 0; p<2; p++) {
    matriz[p] = [];
    for(var q:Number = 0; q<2; q++) {
        matriz[p][q] = 0;
    }
}

答案 2 :(得分:0)

public class cArray
{

    private  var DIM1CAP:uint=0;
    private  var DIM2CAP:uint=1;
    private  var DIM3CAP:uint=2;
    private  var DIM4CAP:uint=3;
    private  var DIM5CAP:uint=4;

    public function cArray():void
    {
        // avoid the noid
    }

    // returns empty array of args.length dimensions
    // 1st argument is dim 1 capacity; 2nd is dim 2, etc.
    public function getArray ( ... args ):Array
    {
        var arr = new Array();

        if ( paramsNotValid(args) )
        {
            return null;
        }

        switch (args.length)
        {
            case 2:
                arr = get2DArray ( args[0], args[1] );
                break;

            case 3:
                arr = get3DArray ( args[0], args[1], args[2] );
                break;

            case 4:
                arr = get4DArray ( args[0], args[1], args[2], args[3] );
                break;

            case 5:
                arr = get5DArray ( args[0], args[1], args[2], args[3], args[4] );
                break;

            default:

                break;
        }



        return arr;
    }


    // returns empty 2d array of parameter specified capacity
    private function get2DArray ( _1stDimCapacity:uint, _2ndDimCapacity:uint ):Array
    {
        var arr2d:Array = [];
        var arr1d = new Array();

        for ( var i:uint=0; i<_1stDimCapacity; i++ )
        {
            arr1d[_2ndDimCapacity-1] = undefined;
            arr2d.push(arr1d);
            arr1d = new Array();
        }
        return arr2d;
    }

    // returns empty 3d array of parameter specified capacity
    private function get3DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint ):Array 
    {
        var arr3d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr3d.push ( get2DArray ( dim2Cap, dim3Cap ) );
        }
        return arr3d;
    }

    // returns empty 4d array of parameter specified capacity
    private function get4DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint,
                                  dim4Cap:uint):Array 
    {
        var arr4d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr4d.push ( get3DArray ( dim2Cap, dim3Cap, dim4Cap ) );
        }
        return arr4d;
    }

    // returns empty 5d array of parameter specified capacity
    private function get5DArray ( dim1Cap:uint, 
                                  dim2Cap:uint, 
                                  dim3Cap:uint,
                                  dim4Cap:uint,
                                  dim5Cap:uint):Array 
    {
        var arr5d = new Array();

        for ( var i:uint=0; i<dim1Cap; i++ )
        {
            arr5d.push ( get4DArray ( dim2Cap, dim3Cap, dim4Cap, dim5Cap ) );
        }
        return arr5d;
    }

    //////////////////////////////////////////////////////
    private function paramsNotValid ( args:Array ):Boolean
    {
        if ( args.length<2 || args.length>5 )
        {
            return true;
        }

        for ( var i:uint=0; i<args.length; i++ )
        {
            if ( ! ( args[i]>0 ) ) 
            {
                break;
            } 
        }

        if ( i < args.length )
        {
            return true;
        }
        return false;
    }

}

}

公共类cMain扩展了MovieClip     {

    var cArr:cArray = new cArray;


    public function cMain():void
    {
        var arr2d:Array;
        var arr3d:Array;
        var chessBrd_4d:Array; 
        var arr5d:Array;

        // capacity of 10 games; 150 moves/gm; white's mv or black's;
        // - piece positions; move in chess notation (index 32 of 
        // - last dimension); commentary (index 33 of last dimension) 
        chessBrd_4d = cArr.getArray(10,150,2,34);

        // adding data
        // - 4th game, 8th move, white's move, positions of pieces
        chessBrd_4d[3][7][0][0] = 'd8';
        chessBrd_4d[3][7][0][1] = 'b1';
        //             ...
        // - positions of pieces up to last one
        //             ...
        // - last piece pos
        chessBrd_4d[3][7][0][31] = 'captured'; 
        // - actual move in chess notation 
        chessBrd_4d[3][7][0][32] = 'nC4';
        // - annotation
        chessBrd_4d[3][7][0][33] = 'blocks b pawn, ' +
                                   'opens diag for c1 bishop, ' +
                                   'Justin Beiber is a putz, ' +
                                   'the president is liar'

        trace ( 'piece 0 is on square ' + chessBrd_4d[3][7][0][0]);
        trace ( 'piece 1 is on square ' +  chessBrd_4d[3][7][0][1]);
        trace ( ' ... ' )
        trace ( 'piece 31 has been ' + chessBrd_4d[3][7][0][31]);
        trace ( 'move: ' + chessBrd_4d[3][7][0][32]);
        trace ( chessBrd_4d[3][7][0][33]);
        /*
        trace results:
        piece 0 is on square d8
        piece 1 is on square b1
         ... 
        piece 31 has been captured
        move: nC4
        blocks b pawn, opens diag for c1 bishop, 
        Justin Beiber is a putz, the president is liar
        */  

        /*
        trace ( chessBrd_4d.length ); = 10
        trace ( chessBrd_4d [ 2 ].length ); = 150
        trace ( chessBrd_4d [ 2 ] [ 4 ].length ); = 2
        trace ( chessBrd_4d [ 2 ] [ 4 ] [ 1 ].length ); = 34
        trace ( chessBrd_4d [ 2 ] [ 4 ] [ 0 ] .length); = 34
        */

    }
}