I found this interesting (and helpful) comparison of arrays vs.
collections in an online tutorial from Macon State College.
- DK
Arrays and Collections
Variables are the basic storage units for holding data values during
processing. A problem arises, however, with large collections of data
values for which it is impractical to declare a separate variable for each
of them. Consider a need to store the names or codes for all the states. It
would require declaring and assigning values to 50 different variables.
Fortunately, though, there is a way to handle these large sets of similar
data items through use of arrays and collections.
Using Arrays
An array is a collection of data values, all of which are accessible
through the same variable name. An array can be thought of as a "table"
with data values occupying its "cells." For instance, you can visualize an
array named Letters containing the first five letters of the Greek alphabet
as in the following table.
|-----------|
| Letters |
| (array) |
|-----------|
| alpha |
|-----------|
| beta |
|-----------|
| gamma |
|-----------|
| delta |
|-----------|
| epsilon |
|-----------|
Index
(0)
(1)
(2)
(3)
(4)
A major advantage of using an array to store a collection of values is that
all values can be referenced through the single array name; it is not
necessary to individually declare and name separate variables for all the
items. Rather, the items in an array are indexed by their position in the
array, numbered beginning with 0 (zero) for the first element in the array.
Thus, the value "gamma" in the above array is identified by the reference
Letters(2). The name of the array is followed by an index value enclosed in
parentheses.
Multidimensional Arrays
Arrays can have more than one dimension to hold pairs, or even triplets, of
values. The following array has two dimensions, the first "column" of which
contains the Greek letters, the second of which contains equivalent English
letters.
|----------+------|
| Letters | |
| (0) | |
| (1) | |
|----------+------|
| alpha | |
| | a |
|----------+------|
| beta | |
| | b |
|----------+------|
| gamma | |
| | c |
|----------+------|
| delta | |
| | d |
|----------+------|
| epsilon | |
| | e |
|----------+------|
Index
(0)
(1)
(2)
(3)
(4)
Now, a particular data value is referenced by a pair of indexes
representing its row and column position, in that order, with both indexes
beginning with 0. The value "gamma" is referenced by Letters(2,0); the
value "e" is referenced by Letters(4,1). The row and column indexes are
separated by a comma.
Declaring Arrays
Similar to declaring variables, an array is declared with a Dim statement.
The array name is followed by a pair of parentheses to indicate an array
variable. For a single dimension array (one column) the parentheses are
empty; for a multidimensional array commas are used for each additional
column. An array is typed in the same way as variables to declare the type
of data to occupy the array. The following statements declare a single- and
a two-dimensional array, respectively, each of which contain string data.
Dim Letters() As String
Dim Letters(,) As String
When arrays are declared, they do not have physical sizes. They have
dimensions, but the number of rows and columns is unknown. If you know in
advance the number of values that will occupy an array, you can include the
size as part of its declaration. The two previous Letters arrays can be
declared as
Dim Letters(4) As String
Dim Letters(4,1) As String
to indicate an array with 5 rows, and an array with 5 rows and 2 columns,
respectively. Note that, because array indexing begins with 0, the
dimensions of an array are always 1 less than the actual number of array
elements. You can determine the actual size of an array through the
arrayName.Length property, which returns the total number of elements in
the array.
Initializing Arrays
There are numerous ways to assign values to the elements of an array. If
the values are known, and they are modest in size, you can populate an
array during its declaration. Values are assigned by enclosing them within
braces, separated by commas. The following statement is one way to
initially load the single-dimension Letters array. (A dimension size is not
required, being determined from the number of values.)
Dim Letters() As String = {"alpha","beta","gamma","delta","epsilon"}
For two-dimensional arrays, pairs of values are loaded by enclosing them
inside braces, which are enclosed inside a pair of braces representing the
array as a whole.
Dim Letters(,) As String = {
{"alpha","a"},{"beta","b"},{"gamma","c"},{"delta","d"},{"epsilon","e"} }
A second method of loading an array of known dimensions is inside a
procedure. The following Page_Load subprogram dimensions an array and loads
values with simple assignment statements. In this case the size of the
array is required since it must have a physical size before assigning
values to its elements.
Sub Page_Load
Dim Letters(4) As String
Letters(0) = "alpha"
Letters(1) = "beta"
Letters(2) = "gamma"
Letters(3) = "delta"
Letters(4) = "epsilon"
End Sub
In a similar way a two-dimensional array can be loaded.
Sub Page_Load
Dim Letters(4,1) As String
Letters(0,0) = "alpha"
Letters(1,0) = "beta"
Letters(2,0) = "gamma"
Letters(3,0) = "delta"
Letters(4,0) = "epsilon"
Letters(0,1) = "a"
Letters(1,1) = "b"
Letters(2,1) = "c"
Letters(3,1) = "d"
Letters(4,1) = "e"
End Sub
Arrays of Unknown Dimensions
It is not unusual that you might not know in advance the number of values
to be loaded into an array. Often times these values are loaded from
external files with changing sizes. One solution is to dimension the array
to some assumed maximum size and don't worry about it being too large.
Another solution is to redimension it to increasing sizes as needed. This
topic is taken up later in the discussion about accessing arrays.
Using Collections
Similar to arrays for storing sets of data items are Visual Basic
collections. A collection is a one-dimensional storage area in which values
of mixed data types can be placed, although there are few occasions for
doing so. Collections are increased in size simply by adding items to it.
A collection is declared with a Dim statement that creates a new Collection
object. The following statement declares a collection for storing the state
codes.
Dim States As New Collection
Then, values are added to the collection with the collection.Add() method.
States.Add("AL")
States.Add("AK")
States.Add("AZ")
States.Add("AR")
States.Add("CA")
...
Values are retrieved from a collection by reference to a collection.Item(
index). Unlike arrays, items in a collection are indexed beginning with 1.
Therefore, a reference to States.Item(5) in the above collection produces
the value "CA". (The keyword .Item is optional when referring to a
collection element; States(5) or States("CA") will work.)
One of the main advantages of collections is that items can be indexed by
values other than their physical locations in the collection. Each item in
a collection can have both a value and a key, where the key is used as the
index to an associated value. Values and keys are established when the
collection is loaded by using the Add() method in the following format.
|--------------------------------------------------------------------------|
| |
| collection.Add(value, key) |
| |
|--------------------------------------------------------------------------|
For example, to load the States collection with state codes and to
establish state names as the keys to them, the following code is used.
Dim States As New Collection
States.Add("AL","Alabama")
States.Add("AK","Alaska")
States.Add("AZ","Arizona")
States.Add("AR","Arkansas")
States.Add("CA","California")
...
Now, to retrieve the state code associated with the state name "California"
the following reference is made,
States.Item("California")
and the value "CA" is retrieved from the collection. (The values and keys
can be reversed to look up a state name from a state code.)
Although collections permit only single-dimension "arrays" of values, they
can be easier to work with than arrays for storing modest collections of
data values. More information about using collections is presented later in
these tutorials.