Usage Summary of ActionScript array

Source: Internet
Author: User
Tags array arrays header
An array this article extracts from my recently produced AS2 Grammar Research Report, for as enthusiasts to communicate, study.

Please correct me where it is wrong or improper, thank you.

Array:
The array type in the AS2 has two places to compare cattle:
1 The Array object itself can hold any data type, not as int A[2],char A[3],someclass a[3] in C + +.
The downside, of course, is immediately emergent, and the object array cannot be created directly.
2 full dynamic array, very simple to use, a bit like vector, but also easier to use error. The dynamic here refers to the size of the array boundary (of course, the dimension is still declared good).

The following is a summary of some of the more important points of the array type, and some of the side-branching stuff is omitted,
Where less talk or say something wrong please be sure to help me point out that everyone together to improve:
1
The following declaration error:

var a[1]:array=new Array ();
function test (Arr[]:array): Void

2 Declaration Mode 1 (note, the declarations here are all using the strongly typed declaration mode)

var a:array=new Array ();
a[0]=0;
A[1]=1;
Trace (a);
Output
0,1

3 Statement Mode 2

var a:array=new Array (4);
A[4]=1;
Trace (a);
Output
undefined,undefined,undefined,undefined,1

Explanation: Dynamic array, with A[4] after length automatically becomes 5.

4 Statement Mode 3
4.1

var b:array=new Array (0,1,2,3);
Trace (b);
0,1,2,3

4.2 does not support nested definitions of this form of two-dimensional arrays

var b:array=new Array ((1,2), (3,4), (5,6);
Trace (b);
Trace (b[0]+ "+b[1]+" "+b[2]+" "+b[3");
Trace (b[0][0]);
2,4,6
2 4 6 undefined undefined
5 replaces the {} in C + + with [];

Declaration Mode 4

var b:array=[[1,2],[3,4],[5,6]];
Trace (b);
Trace (b[0]+ "+b[1]+" "+b[2]+" "+b[3");
Trace (b[0][0]);
1,2,3,4,5,6
1,2 3,4 5,6 undefined//output an array row with b[0.
1

The dimension setting of 62 dimensions is also dynamic:

var b:array=[[1,2],[3,4],[5,6]];
b[2][3]=12;
Trace (b[2][3]);
Output
12

7 other two-dimensional array declaration methods:
7.1 Set the corresponding number of columns by the line header parameter.
//

2*3 Array Declare.
var growindex:number=2;
var gcolindex:number=3;
var a:array=new Array (Growindex);
for (Var i=0;i<growindex;i++)
{a[i]=new Array (gcolindex);
for (Var j=0;j<gcolindex;j++)
{
A[i][j]=string (i) +string (j);
}
}
Trace (a);
Output
00,01,02,10,11,12

8 array as a function of the transfer of parameters.
8.1

var b:array=new Array (0,1,2,3);
function test (Arr:array): Void
{
Trace (arr[0]+ "+arr[1]+" "+arr[2]+" "+arr[3]+");
}
Test (b);
0 1 2 3

8.2
Two-dimensional arrays:

var b:array=[[1,2],[3,4],[5,6]];
function test (Arr:array): Void
{
Trace (arr);
Trace (arr[0]+ "+arr[1]+" "+arr[2]+" "+arr[3]+");
}
Test (b);
1,2,3,4,5,6
1,2 3,4 5,6 undefined

8.3
Passing of line header parameters:

var b:array=[[1,2],[3,4],[5,6]];
function test (Arr:array): Void
{
Trace (arr);
Trace (arr[0]+ "+arr[1]+");
}
Test (b[0]);
Test (b[1]);
Test (b[2]);
1,2
1 2
3,4
3 4
5,6
5 6

9 MORE:
9.1

var growindex:number=2;
var gcolindex:number=3;
var a:array=[[0,1],[3,4,5]];//a location where an element is vacant, here for [0][2]
Trace (a);
for (Var i=0;i<growindex;i++)
{
for (Var j=0;j<gcolindex;j++)
{
Trace (A[i][j]);
}
}
0,1,3,4,5
0
1
Undefined
3
4
5

9.2 Dynamic dimensions do not support

var b:array=[5,6];
b[0][0]=3;
Trace (b[0][0]);
Output
Undefined

9.3 An example of an indirect object array implementation.

var enarray=new Array (3);
------connected to the enemy array---------//
for (Var j=0;j<3;j++) {
Attachmovie ("Baddie", "baddie" +j, 200+j);
ENARRAY[J] = _root["baddie" +j];
enarray[j]._x = 50*j;
enarray[j]._y = 100;
}

9.4
There are also many examples of actual use, such as using an array to save color data, loading a picture's variable name, and so on.


10 Delete array elements:
10.1 One-dimensional:

var p=new Array (1,2,3,4,5);
P.splice (1);
Trace (P);
var t=new Array (1,2,3,4,5);
T.splice (2,1);
Trace (t);
Output
1
1,2,4,5

10.2 Two-dimensional situation:

var b:array=[[1,2],[3,4],[5,6]];
B[0].splice (0);//delete c[0][0] c[0][1]
Trace (b);
var c:array=[[1,2],[3,4],[5,6]];
C[1].splice (1);//c[1][1]
Trace (c);
, 3,4,5,6
1,2,3,5,6

11 Length test:

var b:array=[[1,2,2],[3,4],[5,6,4,5]];
Trace (b.length);
Trace (b[0].length);
Trace (b[2].length);
Output
3
3
4

=========================================================
One months after the supplement:
11 Dynamic Dimensions: The same as in Java.

var arr:array=new Array (1);
Arr[0]=new Array (3);
Arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
Trace (arr);
Trace (arr[0][0]);
Trace (arr[0][1]);
Trace (arr[0][2]);
1,2,3
1
2

12 new ways to load data (multiple attributes):

Mdata=new Array ();
Mdata.additem ({label: "Two ball momentum conservation (one-dimensional)", data:0});
Mdata.additem ({label: "Three-ball momentum conservation (one-dimensional)", data:1});
Mdata.additem ({label: "Multiple ball plane collision (two-dimensional)", data:2});
Mdata.additem ({label: "Child Ball", data:3});

Used in conjunction with the use of ComboBox in V2component:

_root.menucombo.dataprovider=mdata;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.