CS0004: Introduction to
Programming
Arrays
Arrays
So far we have been using simple variables.
Simple variables hold only one value.
Dim x As String = “Banana”
x only has one value: “Banana”
Arrays are indexed lists of simple variables of the same
type
Indexed here means we can access each simple variable by its
index. Indices are usually numbers for arrays.
In other words, arrays hold multiple values (complex variables)
Arrays allow programmers to greatly simplify their code
by logically organizing similar data into one entity.
Arrays
Imagine the following problem:
You want to take in the grades of 30 students and display
which ones had above average scores.
The algorithm could look as follows:
Take in the grades of 30 students with their names
Find the average
Display which students scored above the average
What is the problem here with using simple variables?
Arrays
Instead of using separate simple variables for the names and
scores of the students, we can use arrays.
Dim students(29) As String
Dim grades(29) As Double
This is declaring two arrays each holding 30 elements (or
subscripted variables) one holding values of type String, one
of type Double
Elements (or subscripted variables) are the individual simple
variables in the array.
We can access an individual element by using its index
students(0)
This will access the first element of the students array.
0 in this case is called an index or subscript.
Indexing of arrays starts at 0.
Arrays
General Form of an array declaration:
Dim arrayName(n) As DataType
arrayName follows normal naming conventions
n is called the upper bound.
The upper bound of an array is the last index that can be used in the
array. The size, the number of elements in the array, is the upper
bound plus one.
DataType is the data type of the elements of the array.
When an array is declared, the individual elements are
located successively in memory. This is good for memory
management.
Array Example 1
New Topics:
Arrays
On Form Load Event Procedure
Array Initialization and Implicit Array Sizing
If you know all the values that are going to be stored in an array at the
time you want to create it, you can use implicit array sizing.
Instead of:
Dim winners(3) As String
winners(0) = "Packers"
winners(1) = "Packers"
winners(2) = "Jets"
winners(3) = "Chiefs"
You can do:
Dim winners() As String = {"Packers", "Packers", "Jets",
"Chiefs"}
The compiler knows that the array has 4 elements implicitly because you
specified 4 initial values.
General Form:
Dim arrayName() As DataType = {value0, value1, value2,
…, valueN}
Array Methods
Assume we have the following array:
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
Some useful array methods:
myNumbers.Count
Gives the number of elements in the array (6)
myNumbers.Max
Gives the “largest” element of the array (depends on the type of the array) (9)
myNumbers.Min
Gives the “smallest” element of the array (depends on the type of the array) (1)
myNumbers.First
Gives the first element of the array (4)
myNumbers.Last
Gives the first element of the array (5)
Arrays as Parameters
Just like any other variables, arrays can be used as
parameters:
Sub mySubprocedure(ByVal myNumbers() As
Integer)
Here numbers is a parameter that is an array of
integers
We can call this subprocedure as such:
Dim myNumbers() As Integer = {4, 1, 6, 3,
9, 5}
mySubprocedure(myNumbers)
Arrays and Looping
Arrays have a structure that makes them natural for use
in For…Next loops.
With For…Next loops we can iterate over all the
elements of an array:
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
For i As Integer = 0 To myNumbers.Count - 1
MessageBox.Show(myNumbers(i))
Next
What is the output of this code?
Array Example 2
New Topics:
Array Initialization and Implicit Array Sizing
Array Methods
Arrays as Parameters
Arrays and Looping
Resizing an Array
So far, when you declare an array, its size is set one way or another
Dim winners(3) As String
Dim winners() As String = {"Packers", "Packers",
"Jets", "Chiefs"}
However, you can resize an array after it has been declared
ReDim winners(4)
This would make the array hold 5 values, but DELETE ALL PREVIOUS
VALUES STORED in winners.
To retain all values after resizing, use the Preserve keyword:
ReDim Preserve winners(4)
As a matter of fact, you do not need to specify the size of an array
when you declare it, but you must give it a size with ReDim before
using it in any way:
Dim winners() As String
ReDim winners(4)
Array Example 3
New Topic:
Resizing an Array
For Each Loops
For Arrays there is a more intuitive way to loop through the elements called a For Each
Loop
We can do the following:
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
For i As Integer = 0 To myNumbers.Count - 1
MessageBox.Show(myNumbers(i))
Next
By using a For Each loop:
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
For Each aNumber As Integer In myNumbers
MessageBox.Show(aNumber)
Next
What this will do is iterate through all the elements in myNumbers and assign them to aNumber for
each iteration.
General Form:
For Each variableName As DataType In arrayName
statement(s)
Next
Array Example 4
New Topics:
For Each Loop
Character Data Type
So far in this course we often handled single characters as
Strings of length 1
Dim theLetterA As String = "a"
However, there is a data type specifically designed for
holding one character called Char
Dim theLetterA As Char = "a"c
Notice the c at the end of the double quotes.
We can convert any String into a character array for
easy indexing using the ToCharArray method
Dim myString As String = "banana"
Dim myCharArray() As Char = myString.ToCharArray
Character Array Example 1
New Topics:
Character Array
Searching For an Element in an Array
To find the index of a particular element in an array you can
use the IndexOf method
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
Dim theIndex As Integer
theIndex = Array.IndexOf(myNumbers, 6)
theIndex will be assigned the value of 2 because the index of 6 in
myNumbers is 2
General Form:
numVar = Array.IndexOf(arrayName, value, startIndex)
numVar - the index of the found value
arrayName - the array to search in
value - the value to search for
startIndex - the index to start looking for
numVar will be -1 if the value is not found in arrayName.
Copying an Array
Be careful when copying an array. Assume two arrays
arrayOne and arrayTwo are declared…
arrayTwo = arrayOne
This will copy the reference of arrayOne to arrayTwo.
This means that whatever happens to arrayOne will
happen to arrayTwo and vice versa.
To copy just the values you can do something like this:
For i As Integer = 0 To arrayOne.Length - 1
arrayTwo(i) = arrayOne(i)
Next
Notes
If you attempt to index into an array that is larger than the
size of the array it will cause a run-time error:
Dim myNumbers() As Integer = {4, 1, 6, 3, 9, 5}
Dim aNumber As Integer = myNumbers(8)
This will cause an error because there is no element in myNumbers with
index 8
There are many cool things about arrays in VB we will not be able
to go over such as:
The split and join methods
LINQ
Two-Dimensional Arrays
There are also many topics that the book goes over that are
somewhat related to arrays that we will not be able to go over:
Reading from a text file
Creation of structures