here

advertisement
integer
21
Computer Memory
We can declare, assign
and manipulate
individual variables
with ease…
integer
integer
21
12
integer
23
Computer Memory
If we have a “list” of
variables, we don’t
currently have an
elegant way to handle
this…
integer
21
21 12 23 21
integer
Computer Memory
What we need is a
single variable that can
handle multiple values
(of the same type)
This is an Array
Arrays
An array is declared using the following syntax:
Dim ArrayName(UpperBound) As Datatype
An array’s name obeys the same rules as when declaring a variable: it begins with a letter and
may be followed by any number of letters, underscores, or digits. An array name can be as
small as one letter or as large as 255 letters, underscores, and digits.
Individual values within an array are selected by using an index.
The lowest index of an array is 0, while the upper bound of the array is the highest index in the
array.
An index must be a Short, an Integer, or a Long data type.
The data type is any valid variable like a Short, an Integer, or a String.
Dim shtGrades(3) As Short
shtGrades (short)
Index
0
1
2
3
Values
0
0
0
0
Computer Memory
shtGrades(0) = 21
We can
assign an
individual
variable by
using its
name, and
index.
Index and
subscript are
synonymous
shtGrades (short)
Index
0
1
2
3
Values
21
0
0
0
Computer Memory
shtGrades(0) = 21
shtGrades(1) = 12
shtGrades(2) = 23
shtGrades(3) = 21
shtGrades (short)
Index
0
1
2
3
Values
21
12
23
21
Computer Memory
shtGrades(4) = 21
It is our
responsibility to
make sure that we
are always
addressing a legal
location in the
array.
shtGrades(-1) = 1
would also crash
our program.
shtGrades (short)
Index
0
1
2
3
Values
21
12
23
21
Computer Memory
Dim strSimpsonNames(4) As String
strSimpsonNames(0) = “Homer"
strSimpsonNames(1) = “Marge"
strSimpsonNames(2) = “Bart"
strSimpsonNames(3) = “Lisa"
strSimpsonNames(4) = “Maggie"
Arrays can contain any
variable type
strSimpsonNames (String)
Index
0
1
Values
“Homer” “Marge”
2
3
4
“Bart”
“Lisa”
“Maggie”
Dim blnSimpsonSexIsMale(4) As Boolean
Arrays can contain
any variable type
blnSimpsonSexIsMale(0) = True
blnSimpsonSexIsMale(1) = False
blnSimpsonSexIsMale(2) = True
blnSimpsonSexIsMale(3) = False
blnSimpsonSexIsMale(4) = False
strSimpsonNames (Boolean)
Index
0
1
2
3
4
Values
True
False
True
False
False
strSimpsonNames (String)
Index
0
1
Values
“Homer” “Marge”
2
3
4
“Bart”
“Lisa”
“Maggie”
We can use array variables just like regular variables
bntRedDemo.Text = strSimpsonNames(0) & “ Simpson”
If (strSimpsonNames(3) = “Lisa”) Then
bntRedDemo.Text = “Oldest of the Simpson girls”
End If
But the real power of arrays comes from using them with loops
Dim blnSimpsonSexIsMale(4) As Boolean
blnSimpsonSexIsMale(0) = True
blnSimpsonSexIsMale(1) = False
blnSimpsonSexIsMale(2) = True
blnSimpsonSexIsMale(3) = False
blnSimpsonSexIsMale(4) = False
Dim intLoopCount As Integer
Dim strSex As String = ""
For intLoopCount = 0 To 4
If blnSimpsonSexIsMale(intLoopCount) Then
strSex = strSex & "M"
Else
strSex = strSex & "F"
End If
Next intLoopCount
bntRedDemo.Text = strSex
Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False}
Note that there is
no upper bound
used. This value is
inferred by
counting the
number of items
in the list
It is often convenient to initialize
an array in the same line of code as
you declare the array.
You can initialize an array by
setting it equal to the values you
wish to initialize the array to
enclosed within curly braces and
separated by commas.
Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False}
Dim intLoopCount As Integer
Dim strSex As String = ""
For intLoopCount = 0 To Ubound(blnSimpsonSexIsMale)
If blnSimpsonSexIsMale(intLoopCount) Then
strSex = strSex & "M"
Else
strSex = strSex & "F"
End If
Next intLoopCount
bntRedDemo.Text = strSex
It is convenient to use the UBound function to
determine the upper bound of an array.
By passing an array to UBound, the upper bound of the
array is returned.
Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"}
Dim intLoopCount As Integer
For intLoopCount = 0 To UBound(strSimpsonNames)
bntRedDemo.Text = strSimpsonNames(intLoopCount) & " Simpson"
Next intLoopCount
Note: Without a call to the Sleep function, this happens
so fast all we see is “Maggie Simpson”
….
Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"}
Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False}
Dim intLoopCount As Integer
For intLoopCount = 0 To UBound(strSimpsonNames)
If blnSimpsonSexIsMale(intLoopCount) Then
bntRedDemo.Text = "Male : " & strSimpsonNames(intLoopCount) & " Simpson"
Else
bntRedDemo.Text = "Female : " & strSimpsonNames(intLoopCount) & " Simpson"
End If
Next intLoopCount
….
Useful Exercises
Can you extend the code above to count the number of males
in the Simpson family?
Can you add a new Array variable to hold the Simpson's
ages, then find the average age of a Simpson family
member?
Can you produce this output…
Homer Simpson
Male: At 36 he is
consider old
….
Maggie Simpson
Female: At 2 she
is consider young
Homer Simpson
Male: At 36 he is
consider old
….
Maggie Simpson
Female: At 2 she
is consider young
Download