Title: Arrays
1Arrays
2Outline
IntroductionArraysDeclaring and Allocating
ArraysExamples Using Arrays Allocating an
Array Initializing the Values in an
Array Summing the Elements of an
Array Using Arrays to Analyze Survey
Results Using Histograms to Display Array Data
GraphicallyPassing Arrays to ProceduresSorting
Array Searching Arrays
3 Introduction
- Arrays
- Arrays are data structures consisting of data
items of the same type. - Static entities
- They remain the same size once they are created
- Position number
- Values that indicate specific locations within
arrays - The first element in every array is the zeroth
element
4 Arrays
Name of array (note that all elements of this
array have the same name, numberArray)
-45
numberArray(0)
6
numberArray(1)
0
numberArray(2)
72
numberArray(3)
1543
numberArray(4)
-89
numberArray(5)
0
numberArray(6)
62
numberArray(7)
-3
numberArray(8)
1
numberArray(9)
Position number (index or subscript) of the
element within array numberArray
6453
numberArray(10)
78
numberArray(11)
Fig. 7.1 Array consisting of 12 elements.
5 Arrays
- All arrays have access to the methods and
properties of class System.Array, including - Length property
- numberArray.Length 12
- GetUpperBound method
- Returns the index of the last element in the
array - The value returned by this GetUpperBound is one
less than the value of the arrays Length
property - numberArray.GetUpperBound(0) 11
6 Declaring and Allocating Arrays
- Memory
- The amount of memory required by an array depends
on the length of the array and the size of the
data type of the elements in the array - The declaration of an array creates a variable
that can store a reference to an array but does
not create the array in memory. - To declare an array, the programmer provides the
arrays name and data type. The following
statement declares numberArray. - Dim numberArray As Integer()
The parentheses that follow the data type
indicate that numberArray is an array.
7 Declaring and Allocating Arrays
- Before the array can be used, the programmer must
specify the size of the array and allocate memory
for the array, using Keyword New - Declaration
- Dim numberArray As Integer()
- Allocation
- numberArray New Integer(11)
- Array bounds Determine what indices can be used
to access an element in the array. Here ( 0 11)
8 Declaring and Allocating Arrays
numberArray New Integer(11)
- The braces ( and ) are called an initializer
list and specify the initial values of the
elements in the array. - When the initializer list is empty, the elements
in the array are initialized to the default value
for the data type of the elements of the array. - The default value is 0 for numeric primitive
data-type variables, False for Boolean variables
and Nothing for references.
9 Declaring and Allocating Arrays
- Visual Basic can determine the array bounds from
the number of elements in the initializer list. - Thus, it is not necessary to specify the size of
the array when a non-empty initializer list is
present. - Examples
- Dim numbers As Integer()
- numbers New Integer() 1, 2, 3, 6
initialization - Dim numberArray As Integer() New Integer(11)
- Dim array1, array2 As Double()
10 Declaring and Allocating Arrays
- How can we set up a Non-Fixed size array?
- First set up an array with empty brackets
- Dim numbers( 10) As Integer
- We can then use other value to reset the array.
You reset an array by using the ReDim word. - You then specify the new value. Like this
- ReDim numbers(20)
11 Examples Using Arrays
- Several examples that demonstrate
- Declaration
- Allocation
- Initialization of arrays
12- 1 ' Fig. 7.2 CreateArray.vb
- 2 ' Declaring and allocating an array.
- 3
- 4 Imports System.Windows.Forms
- 5
- 6 Module modCreateArray
- 7
- 8 Sub Main()
- 9 Dim output As String
- 10 Dim i As Integer
- 11
- 12 Dim array As Integer() ' declare
array variable - 13 array New Integer(9) ' allocate
memory for array - 14
- 15 output "Subscript " vbTab
"Value" vbCrLf - 16
- 17 ' display values in array
- 18 For i 0 To array.GetUpperBound(0)
- 19 output i vbTab array(i)
vbCrLf
Allocate an array of 10 Integer elements, which
are initially zero. The program displays the
array elements in tabular format in a dialog.
13 Examples Using Arrays
14- 1 ' Fig. 7.3 InitArray.vb
- 2 ' Initializing arrays.
- 3
- 4 Imports System.Windows.Forms
- 5
- 6 Module modInitArray
- 7
- 8 Sub Main()
- 9 Dim output As String
- 10 Dim i As Integer
- 11
- 12 Dim array1, array2 As Integer() '
declare two arrays - 13
- 14 ' initializer list specifies number of
elements - 15 ' and value of each element
- 16 array1 New Integer() 32, 27, 64,
18, 95, _ - 17 14, 90, 70, 60, 37
- 18
- 19 ' allocate array2 based on length of
array1
Creates two integer arrays of 10 elements each
and sets the values of the elements, using an
initializer list and a For structure.
One statement is used to declare the two arrays
Allocates the 10 elements of array1 with New and
initialize the values in the array, using an
initializer list
Allocates array2, whose size is determined by
arry1.GetUpperBound(0), so that array1 and array2
have the same upper bound
Initializes each element in array2 to the even
integers
15Uses the values in the arrays to build String
output, which is displayed in a MessageBox
- 26
- 27 output "Subscript " vbTab
"Array1" vbTab _ - 28 "Array2" vbCrLf
- 29
- 30 ' display values for both arrays
- 31 For i 0 To array1.GetUpperBound(0)
- 32 output i vbTab array1(i)
vbTab array2(i) _ - 33 vbCrLf
- 34 Next
- 35
- 36 MessageBox.Show(output, "Array of
Integer Values", _ - 37 MessageBoxButtons.OK,
MessageBoxIcon.Information) - 38 End Sub ' Main
- 39
- 40 End Module ' modInitArray
16- 1 ' Fig. 7.4 SumArray.vb
- 2 ' Computing sum of elements in array.
- 3
- 4 Imports System.Windows.Forms
- 5
- 6 Module modSumArray
- 7
- 8 Sub Main()
- 9 Dim array As Integer() New Integer()
_ - 10 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- 11
- 12 Dim total As Integer 0, i As Integer
0 - 13
- 14 ' sum array element values
- 15 For i 0 To array.GetUpperBound(0)
- 16 total array(i)
- 17 Next
- 18
- 19 MessageBox.Show("Total of array
elements " total, _
Sums the values contained in a 10-element
integer array.
17- 1 ' Fig. 7.6 Histogram.vb
- 2 ' Using data to create histograms.
- 4 Imports System.Windows.Forms
- 5 Module modHistogram
- 7 Sub Main()
- 9 Dim output As String ' output
string - 10 Dim i, j As Integer ' counters
- 11
- 12 ' create data array
- 13 Dim array1 As Integer() New
Integer() _ - 14 19, 3, 15, 7, 11, 9, 13, 5, 17, 1
- 15
- 16 output "Element " vbTab "Value
" vbTab _ - 17 "Histogram"
- 18
- 19 For i 0 To array1.GetUpperBound(0)
- 20 output vbCrLf i vbTab
array1(i) vbTab - 21
- 22 For j 1 To array1(i)
Inner For structure counts from 1 to array(i),
which is the value in the ith index of array1
18(No Transcript)
19Array of Controls
- We can define an array of controls, using the
same statements used to declare a variable array.
For example, to declare an array of 20 buttons,
use - Dim btnArray As Button() New Button(20)
20Array of Controls
- Say we have 10 check boxes (chkBox01, chkBox02,
chkBox03, chkBox04, chkBox05, chkBox06, chkBox07,
chkBox08, chkBox09, chkBox10) on a form and we
need to examine each check boxs Checked
property. If that property is True, we need to
process 30 lines of additional code. - For one check box, that code would be
- If chkBox01.Checked Then
- do these 30 lines of code
- End If
- We would need to repeat this 9 more times (for
the nine remaining check boxes), yielding a total
of 32 x 10 320 lines of code.
21Array of Controls
- Heres the solution. Define an array of 10 check
box controls and assign the array values to
existing controls - Dim MyCheck As CheckBox() new CheckBox(10)
- MyCheck(1) chkBox01
- MyCheck(2) chkBox02
- MyCheck(3) chkBox03
- MyCheck(4) chkBox04
- MyCheck(5) chkBox05
- MyCheck(6) chkBox06
- MyCheck(7) chkBox07
- MyCheck(8) chkBox08
- MyCheck(9) chkBox09
- MyCheck(10) chkBox10
Dim I As Integer . . For I 1 To 10 If
MyCheck(I).Checked Then do these 30 lines of
code End If Next I
22Array of Controls
- The 320 lines of code have been reduced to about
45 (including all the declarations) and code
maintenance is now much easier. - Obviously, it is not necessary to use control
arrays, but they do have their advantages.