I originally wanted to write a tutorial on arrays by myself. As a result, today I found a good tutorial on arrays, saving my effort. Author: FlashGuru
If you are interested in arrays, you must understand the variables. A variable is a container containing data. The data can be numbers, strings, or Boolean values. (0312: Number. I don't need to explain the character string. The Boolean value can only be false or true. It is usually used to control the program through a logical operator.)
Arrays are similar to variables as data containers, but they can contain more data. Each element (a part of the data) is attached to an index.
Arrays can be used to save your scripts and organizational structures. They are usually used to organize some associated values that are separated by an index value from other elements in the array. You can use the following method to define three variables ::
Quote1 = "Flash is cool! "Quote2 =" Flash is my favorite program "(0312: these are strings.) Quote3 = "Flash rules"
Obviously there is a better way to implement the above example, that is to use arrays. There are many ways to create an array, but I first demonstrate the simplest method:
Name_of_array = new Array ()
So if we want to reference an array, use this:
Quotes = new Array ()
Well, now we have an array, but the problem is that there is no data in it, it is useless, so now we put the data in our array, we use an index number to define its position in the array.
We can put the first element in the array using the following method:
Quotes [0] = "Flash is cool! "
What I want to talk about is in FlashArray index subscript is based on 0Which meansThe index value of the first element in the array is 0.Therefore, we can put a data in the second element of our array:
Quotes [1] = "Flash is my favorite program "(0312: the subscript of the first element in the array is 0. The subscript of the second element is 1..)
The following syntax can be used to fill all elements in the array:
Name_of_array [index] = value
Note:The name_of_array above is the name of your array, and index is the position where you want to add data to your array.So we created code using variables earlier:
Quote1 = "Flash is cool! "Quote2 =" Flash is my favorite program "quote3 =" Flash rules"
It can also be written as follows:
Quotes = new Array () quotes [0] = "Flash is cool! "Quotes [1] =" Flash is my favorite program "quotes [2] =" Flash rules"
We can also create the following syntax:
Name_of_array = new Array ("value1", "value2", "value3 ")This is implemented in a line of code,Value1 is the first element in the array. value2 is the second element in the array, and so on.We use the following syntax to create the referenced array:
Quotes = new Array ("Flash is cool! "," Flash is my favorite program "," Flash rules ")
Now we know how to create an array and how to use it. In the following syntax, we can also create a Shujie:
Name_of_array = ["value1", "value2", "value3"]
This is different from the previous one because the Array object (new Array () is not used. Therefore, we can create the referenced Array code as follows:
Quotes = ["Flash is cool! "," Flash is my favorite program "," Flash rules "]
No matter what method you use, I still like to use this last method to create an array, which is so simple. Now we know how to create an array, but sometimes you want to access your array in the script. Fortunately, there is another way to implement it:
Mynewvariable = name_of_array [index](0312: pay attention to the meaning of each word)
To attach a value to a new variable as the value of the first element in the array, do the following:
Firstquote = quotes [0]
The new value named firstquote is "Flash is cool !"
In the script, implement the array. Suppose we want to get all the elements in the array, but we don't know how many elements are in the array. Fortunately, we set a number of elements for the array object in Flash.Attributes and methods, One of the attributes isLength indicates the number of elements in the array.Syntax:
Myarraylength = name_of_array.length
The above code creates a new variable, and the value in it is the number of elements of the array (name_of_array) you specify. When we reference an array, use the following code to obtain the number of elements in the array: If quotes is an array, thenLen = quotes. length the len variable value is the length of the array
The length of the array is equal to the value of its highest index plus 1. If the maximum index value is 2, the length of the array is 3, that is, it has three elements, because the array subscript in Flash 5 is based on 0.
Now we know how to use the Length attribute of the array. The following script can be used to obtain the values of all elements in the number level.
For (z = 0; z let's analyze the above code step by step. First, a temporary variable z is created.
Z = 0
The following code is a loop condition. When the variable z is smaller than the element length of the array, it loops.
ZThen add 1 to the variable z increment; ++ z
You can put the code you need here and it will be executed (the value of name_of_array.length) times.
For (z = 0; z
Associative arrays:
Associating an array provides another method for storing information. You can use a string as the subscript of an array instead of a number, which makes it easier to remember.In this case, it cannot be used in a loop because it is not a numerical value. (0312: I suspect that the example used to explain the for in statement last time is an associated array, but the syntax is incorrect. It is being studied ....)
The definition of the associated array is the same as that of the alternative array, but the only difference is that it uses a string as the index subscript.
See the following example to define an array of cars:
Mycars = new Array () mycars [0] = "peugoet 306" mycars [1] = "citreon saxo" mycars [2] = "ford fiesta"
This is the format of the associated array:
Mycars = new Array () mycars ["fast"] = "peugoet 306" mycars ["sporty"] = "citreon saxo" mycars ["old"] = "ford fiesta"
It is sometimes useful to index values with characters, but it is inconvenient to retrieve arrays in a loop.
Now, to get the value in this type of array, you have to use a string as the index. Refer to the above example to write it:
Mysportycar = mycars ["sporty"]
The result is that the value of mysportyca is "citreon saxo"