|
Title: Introduction to Arrays
Level: Beginner
Author: Benjie Moss
Website: www.232.info
This is the first of two tutorials focusing
on the use of arrays in actionscript. Assuming no knowledge
of arrays part one of this tutorial deals with methods of
array creation. Part two of this tutorial deals with
using ‘for’ loops to reference arrays by index
number. Part three deals with manipulating arrays using
two basic array methods. This tutorial is intended as
an introduction to the second tutorial and does not cover
everything associated with arrays. The second tutorial
covers referencing arrays by content, multi-dimensional
arrays and looks at array methods in more depth. This
tutorial can be used with both Flash 5 and Flash MX.
Part One: Creating Arrays
Part Two: Using Arrays
Part Three: Manipulating
Arrays
I'm going to get this off my chest right now: Arrays
are not a handy little add-on to actionscript, if
they weren't supported, someone would need to find
a way of making them work. They are a key programming
concept across a multitude of languages and arguably almost
as significant as variables.
Officially an array is a composite datatype,
but phrased simply, it's a list of information.
Like a variable it can contain any legal data. In order
to use it, we assign it to a variable. fig.1
shows, on it's left hand side a series of variables
and on it's right hand side the array equivalent.
Just like a variable an array can be reasigned a value,
so the script in fig.2 is largely pointless. Because
the arrays all have the same name all this script does is
change the value of the variable person, so that only
the last array is the value of ‘person’ that
we could subsequently refer to. It does however illustrate
that there are numerous ways to create an array and populate
it with elements.
Line two illustrates assigning an
empty array to a variable, which would initially seem
pointless, however, if you were to follow this line
with some actionscript that dynamically populated the array
this could be the way to go.
Line four illustrates assigning
a populated array to a variable and is probably the most
common form of array creation.
Line six illustrates that creating
an array with empty elements is possible. If you were
to test the length of this array the result would be 6,
even though no data is contained within. One of the
possible uses for this method of array creation is when
used in conjunction with a loop that runs the length of
the array (I'll be getting to that shortly).
Lines eight & nine demonstrate
that it's possible to include expressions as data in
an array, just as it is with variables.
Line eleven Illustrates array literals,
which are my favourite. Although there's no real
benefit to creating arrays in this way, when I type
a lot of actionscript with a lot of brackets surrounding
equations I find the square brackets stand out more easily
when I've forgotten to comment my code properly.
Lines thirteen & fifteen illustrate
that it is possible to nest arrays. In the example,
I've used my profile as it might exist in a list of users.
One would hope that any site that needs a list of users
would have more than just myself registered (sad case
of affairs otherwise). So the natural way to order
this information would be to have an array for each user,
and then store all those arrays in one large array.
Next
|