Arrays

Arrays
Introduction

This chapter introduces basic concepts of data
structures—collections of related data items.

Arrays are simple data structures consisting only of
data items of the same type.

Arrays normally are “static” entities, in that they
typically remain the same size once they’re created,
although they can be resized
Arrays

An array is a group of variables (called elements)
containing values that all have the same type.

To refer to a particular element, we specify the array’s
name and the position number of the element to which
we refer.

The first element in every array is the zeroth element.

The highest position number in array is called the
array’s upper bound

The position number in parentheses more formally is
called an index and must be a nonnegative integer or
integer expression.
Declaring and Allocating
Arrays

The following statement can be used to declare the
array in Fig. 7.1:

Dim c(11) As Integer

The parentheses that follow the variable name indicate
that c is an array.

Arrays can be declared to contain elements of any type;
every element of the array is of that type.

The number in parentheses in the array declaration
helps the compiler allocate memory for the array c.
Declaring and Allocating
Arrays

Initializer Lists

You can follow an array declaration with an equal sign and an
initializer list in braces, { and }, to specify the initial values of
the array’s elements.

For instance,

Dim numbers() As Integer = {1, 2, 3, 6}
declares and allocates an array containing four Integer
values.

Default Initialization

When you do not provide an initializer list, the elements in the
array are initialized to the default value for the array’s type—0
for numeric primitive data-type variables, False for Boolean
variables and Nothing for String and other class types.
Arrays

Array Length

Every array “knows” its own length (that is, number of
elements), which is determined by the array’s Length
property, as in:

c.Length

All arrays have the methods and properties of class
System.Array.

Array method GetUpperBound returns the index of the last
element in the array.

The value returned by method GetUpperBound is one less
than the value of the array’s Length property.

For one-dimensional arrays, the argument passed to
GetUpperBound is always 0.
Summing the Elements of an
Array

Often, the elements of an array represent a series of
related values that are used in a calculation.
Exceptions with Arrays

An exception indicates a problem that occurs while an app
executes.

The name “exception” suggests that the problem occurs
infrequently—if the “rule” is that a statement normally
executes correctly, then the problem represents the
“exception to the rule.”

Exception handling enables you to create fault-tolerant
programs that can resolve (or handle) exceptions.
Using Array Elements for
Accumulators
Using List Boxes With Arrays
Allow the user to select
from a list and the
SelectedIndex property
can be used as the
subscript of the total array.
IndexInteger = GroupListBox.SelectedIndex
If IndexInteger <> –1 Then
Passing an Array to a Method

To pass an array argument to a method, specify the
name of the array without using parentheses.

For example, if array hourlyTemperatures has been
declared as
Dim hourlyTemperatures(24) As Integer
the method call
DisplayDayData(hourlyTemperatures)
passes array hourlyTemperatures to method
DisplayDayData.
Passing an Array to a Method

Every array object “knows” its own upper bound (that
is, the value returned by the method GetUpperBound),
so when you pass an array object to a method, you do
not need to pass the upper bound of the array as a
separate argument.

For a method to receive an array through a method call,
the method’s parameter list must specify that an array
will be received.

For example, the method header for DisplayDayData
might be written as

Sub DisplayDayData(temperatureData() As Integer)
indicating that DisplayDayData expects to receive an
Integer array in parameter temperatureData.
Passing an Array to a Method

When you pass an array to method DisplayDayData, a
copy of the array’s reference is passed and the method
can change the original array’s element values.

Individual array elements can be passed by value or by
reference like simple variables of that type.

For instance, array element values of primitive types,
such as Integer, can be passed either by value or by
reference, depending on the method definition.
For Each…Next Repetition
Statement

The For Each…Next repetition statement iterates
through the values in a data structure, such as an array,
without using a loop counter.

VB references EACH element of the array and assigns its
value to ElementName.

Variable used for ElementName must be same datatype as
array elements or an Object datatype.

Best to declare the variable for ElementName as part of the
For Each statement to create a block-level variable

Makes one pass through the loop per element

Use Exit For statement within loop to exit early.
The For Each and Next
Statements
For Each ElementName [As Datatype] In ArrayName
' Statement(s) in loop.
Next [ElementName]
For Each OneNameString As String In NameString
' Write one element of the array.
Debug.WriteLine(OneNameString)
Next OneNameString
Multidimensional Arrays

The arrays we’ve studied so far are one-dimensional
arrays—they contain a list of values and use only one
index to access each element.

Multidimensional arrays, also know as rectangular
arrays, require two or more indices to identify
particular elements.

Each row is the same size, and each column is the same
size (hence the term “rectangular”).

To identify a particular table element, we specify two
indices—by convention, the first identifies the element’s
row, the second the element’s column.
The Dim Statement for Two-Dimensional
Arrays — Example(s)

Dim NameString(2, 3) As String

Dim NameString( , ) As String = {{"James", "Mary", "Sammie",
"Sean"}, _ {"Tom", "Lee", "Leon", "Larry"}, {"Maria", "Margaret",
"Jill", "John"}}

' Both statements establish an array of 12 elements.
(0, 0)
James
(0, 1)
Mary
(0, 2)
Sammie
(0, 3)
Sean
(1, 0)
Tom
(1, 1)
Lee
(1, 2)
Leon
(1, 3)
Larry
(2, 0)
Maria
(2, 1)
Margaret
(2, 2)
Jill
(2, 3)
John
Nested For/Next Example
For RowInteger As Integer= 0 To 2
For ColumnInteger As Integer= 0 To 3
' Initialize each element.
NameString(RowInteger, ColumnInteger) = " "
Next ColumnInteger
Next RowInteger
Resizing an Array with the
ReDim Statement

An array’s size cannot be changed, so a new array must be
created if you need to change the size of an existing array.

The ReDim statement “resizes” an array at execution time by
creating a new array and assigning it to the specified array
variable.

The old array’s memory is eventually reclaimed by the runtime.

To save the original data stored in an array, follow the ReDim
keyword with the optional Preserve.