Arrays
An array is a way to store data in a set to save
space and to have an easier name for individual items that form a set
of variables. An example of a set is quarters of a game – 1st
quarter, 2nd quarter, 3rd quarter and 4th
quarter. For a program to keep track of the final score in each
quarter, four separate variables would be required, and they all would
have a similar name.
Defining an Array
Quarter can be declared as an array, given one
name. Each individual quarter can be referred to an element within
the array.
Syntax:
Dim ArrayName(number)
as type
The ArrayName is what you name the array.
Number is the number of elements the array will
hold.
Type is the type of data the elements will be.
Example:
Dim Quarter(4) as
Integer
Visual Basic uses Zero Based arrays, the
following integer variables are created: Quarters(0), Quarter(1),
Quarter(2) and Quarter(3)
The default is for arrays to be public. If you
declare an array in a module, you will need to use the keyword Public
rather than Dim for it to be treated as public.
Zero Based Arrays
The problem is immediately visible. Quarter(3)
will be the name for the score in the 4th quarter. To get
around this, I declare an array with one extra element, so that I can
refer to the elements in a way that makes sense. I end up with a
variable I’ll never use, Quarter(0), but that’s OK with me. So I’d do
it like this:
Dim Quarter(5) as
Integer
Using an Array
Here’s where you really see the power of an
array. The array element number can be substituted with another
variable and action can be taken on it as a group using looping
statements.
Example:
For x = 1 to 4
TotalScore = TotalScore + quarter(x)
Next
Multidimensional Arrays
A one dimensional array is a list, a two
dimensional array is a table. Three dimensional arrays are also
possible. This kind of an array would be a mathematical model of some
kind.
Syntax:
Dim Scoreboard(1,3) as
integer
This would create a table with 2 rows and 3
columns. You could use this to record the scores in each quarter for
2 teams.
When working with the individual elements, you
can think of these elements in terms of a table. This one would have
2 rows – one for each team, and 4 columns, one each quarter.
Reference these table cells as
Scoreboard(row, column).
Many years ago, there was a soft drink called RC
cola. We used this to remember row column referencing when
programming arrays.
A sample of a scoreboard program is here.
ReDim
In some cases, you will not know the dimensions
of the array you are creating. An example is the Scoreboard. What
happens if you go into overtime?
When you find that a condition occurs where you
will need to reconfigure your list or table by adding more cells, use
the ReDim statement.
Syntax:
ReDim ArrayName(number)
Example:
If OverTime = “yes” then
ReDim Scoreboard(1,4)
End if
The Array Class
There are additional methods that can be used
with arrays including Array.Sort, Array.Find, Array.Reverse,
Array.Copy and Array.Clear.
Syntax:
Array.Sort(ArrayName)
Other Comments
Many textbooks explain arrays in terms of memory
allocation. This may be a tradition, but I never found it helpful to
think about how the memory is used to store an array. I just need to
know how to reference the elements. Thinking about the one dimension
array as a linear set of boxes works for me. Thinking about the two
dimensional array as a table with rows and columns works for me too. I
know they are in memory, and it isn’t useful to bring memory into
story. I’m not sure exactly how items are placed in memory, but I
think they all follow each other one after the other and that no table
form is ever created in memory. The analogy of referencing a table
cells is useful; thinking about memory is not.
|