Programming with Parallel Arrays
An array is a data structure
that can store many different values using one variable name.
Each different value is stored in an “element” of the array,
and these elements are numbered for accurate storage and retrieval.
If
you want to store different values in one column than in the other
use “parallel
arrays.”
For example, a grocery list contains “grocery” items
that are “String” type data,
and may also contain the price of each item, which is “Currency.”
These two pieces of data are
related but cannot be stored in the same array.
Two lists can be made, one for each item’s description
and the other for the item’s price.
Note that there will be the same number of items in each list
and that they should be kept in the
same order.
Each item description
corresponds to its price in the other list.
So, instead of using a“2-dimensional” array, we can use 2 “1-dimensional” arrays :
Description (1) =
|
Bread |
|
Description (2) = |
Butter |
|
Description (3) = |
Milk |
|
Description (4) = |
Eggs |
|
Description (5) = |
Sugar |
Dim Price (1 To 5) As Currency
Price(1) =
|
1.79 |
|
Price(2) = |
2.99 |
|
Price(3) = |
4.59 |
|
Price(4) = |
2.19 |
|
Price(5) = |
1.89 |
Here
are two corresponding lists, and the first item in the top list
is the description of the item whose price is in the top row of the other list.
So,
Price(1) is the price of the item in Description (1),
and Price(2) is the price of the item in
Description (2),
and so on
The corresponding elements of each array
can be visually ”lined up” in your mind’s eye like this :

The price of each
item correlates to the description on the same row.
Both arrays need to be the same length
and the data needs to be managed properly (programmatically)
to keep the data in each array “lined up” in this manner.
In this case, the
data in each array is of a different type,
and therefore this implementation requires two separate arrays.
If the two “columns”
of data were of the same type,
this could be accomplished using a two-dimensional array.