This is the first lesson so we will talk about what arrays are, how to use them,
and make our own simple one dimensional array.
What is an array?
An array is kind of a data type, like a string, variable and so on, which means
its purpose in life is to contain data. An array is usually made to contain more
then one information, like a list of information. We could load a list of objects
into and array.
One thing I use arrays to a lot is to hold data sets from a database, or an
xml file, so I can close my database connection after the data is loaded,
and only needs to call the array data when needed. Its much easier and
much faster, I will get into that in one of the more advanced array lessons.
How to make and use an array?
Now here is how to declare a simple array list, and put some simple one
dimensional data into it.
This is the most simple form of an array, here is an example.
var myArray:Array = ["google", "yahoo", "orkut"];
trace(myArray[0]);
trace(myArray[1]);
trace(myArray[2]);
What we did here was to declare a variable with the name “myArray”
and give it 3 data items, google, yahoo and orkut.
You may have noticed how easy it is to trace an array item just by calling
the myArray[index].
If you think this was simple, just wait to see how simple it is to add items
and much more.
myArray.push("msn");
That”s it, now lets bring it all together, lets try to add
a couple of items to the list, then output all the items
with a loop, we will count how many items the array holds,
then loop through each item.var myArray:Array = [];
myArray.push("google");myArray.push("yahoo");myArray.push("orkut");myArray.push("msn");myArray.push("ask");myArray.push("facebook");for(var i:uint = 0; i < style="color: rgb(51, 51, 255);">length; i++)
{
trace(myArray[i]);
}
This will output all items >> goolge, yahoo, orkut, msn,
ask, facebook
No comments:
Post a Comment