Managing Related Data: The Array Object Flash

Have you ever had a bunch of variables that have a lot in common? For example, do you have variables such as name_1, name_2, name_3, name_4, and so on? These variables look like lists of common information, such as:

In programming languages, an array is a list of values that can be addressed by their position in the list. An array is created by using the Array constructor:

visitors = new Array();

The preceding code object simply creates the array container for data. You create an array with information already specified, such as:

visitors = new Array(“John”,”Vanessa”,”Jennifer”,”Frank”);

To access an item in visitors, you would use the array access operators with an array index number. To access the first position’s data, you would use the following code:

message = “Hello “ + visitors[0] + “, and welcome.”;

Here, visitors[0] will return the value “John”. If you traced the message variable, it would read:

Hello John, and welcome.

In most programming languages, the first index value (the starting position) is 0, not 1. In the following table, you’ll see how the index number increases with our sample visitors array.

You can set and get the values within an array using the array access operators. You can replace existing array values by setting the index position to a new value, and you add values to the array by increasing the index number, as in:

In the example, “Nicole” replaces “Frank”, and “Candice” is added to the end of the array. You can also add elements to the array using the push method of the Array Object, as in:

This code will add “Nicole” and “Candice” after “Frank”, and set the variable newLength equal to the length of the visitors array. length is an Array property that returns the number of elements in the array. In the preceding example, newLength is equal to 6, because there are now six names in the array.



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Flash Topics