ActionScript 3.0 Array Operations

Source: Internet
Author: User

VaR arr: array = new array ();
Arr = ["A", "B", "C"]; // assign an initial value. Note that even if a single character is assigned a value ""
Trace (ARR [1]); // obtain member information
Trace (ARR. Length); // gets the number of elements contained in the array

// Use the for loop to output each element of the array one by one. This is also called array traversal.
VaR looptime: Int = arr. length;
For (var I: Int = 0; I <looptime; I ++)
{
Trace (ARR [I]);
}


// Add new elements to the end of the array and use the push () method
Arr. Push ("D ");
Trace (ARR); // returns a, B, c, d

// Insert a new element into the beginning of the array using the unshift () method
Arr. unshift ("e ");
Trace (ARR); // returns E, A, B, C, D

// Delete the element starting with an array. Use the shift () method to delete the last element and use the POP () method.
Arr. Shift ();
Trace (ARR );

Arr. Pop ();
Trace (ARR );

Trace ("\ n ");

/* To delete an element at any position in the array, use the splice () method.
There are two parameters for the method. The first parameter determines the location where the deletion starts and the second parameter is set.
The parameter determines the number of elements to delete .*/
Arr. splice (1st); // delete one element after the first array element
Trace (ARR); // output a, c

/* The splice () method not only deletes array elements, but also adds elements to the array.
When the value of the 2nd parameters of this method is 0, it indicates the position determined by the 1st parameters.
Then insert the element. The inserted value can be passed into the splice () method as a subsequent parameter .*/
Arr. splice (1st, "B"); // Insert the element "B" after elements"
Trace (ARR); // returns a, B, c



// To connect the two arrays, create a new array and use the Concat () method of the array
VaR arr1: array = [1, 2, 3];
VaR arr2: array = [4, 5, 6];
VaR arr3: array = arr1.concat (arr2) // note that this method statement does not have a semicolon
Trace (arr3 );


// To reverse the element order in the array, use the reverse () method of the array.
Trace (arr3.reverse ());

// The Data Types of elements in the array can be different from each other, for example
VaR arr4: array = ["A", 123, false]; // 1st elements are string a, 2nd are numbers 123, and 3rd are boolean values false

Trace ("\ n ");
// Multi-dimensional array var arr0: array = [[, 3], [, 6], [, 9];


/* Some objects can also be stored in the array. You can access the attributes of the array element.
To obtain the corresponding data. The code of this method is clear and clear, which is very practical in actual work */
VaR arr5: array = [{name: "Lucy", age: 21 },
{Name: "Mike", age: 22 },
{Name: "toky", age: 20}
];
Trace (arr5 [0]. Name );
Trace (arr5 [2]. Age );

Trace ("\ n ");


/* For the case where array elements are objects, flash provides the sorton () method
Sort the attributes of object elements in the array. The following code sorts the attributes in the arr5 array:
Elements are sorted by age, and the name value of the element with the smallest age value is output */
VaR arr6: array = arr5.sorton ("Age ")
Trace (arr6 [0]. Name );

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.