Transcript BACS 287

BACS 287
Arrays, Collections,
Generics, and other
Data Structures
BACS 287
Data Structures


All programming languages provide a means
to structure data in your programs.
Visual Basic provides three primary memory
based data structures:
–
–
–
Arrays
Collections
Generics
BACS 287
Arrays




An array is a group of related variables that
share the same name.
The individual variables in the array are
called elements. Elements hold values.
Each element is identified by an index
number (subscript).
Arrays may have more than one subscript,
each specifying the range of a different
dimension.
BACS 287
One-Dimension Arrays
Dim Weekdays(6) as String
Total of 7 elements (0 to 6)
0
1
2
3
4
5
6
Mon
Tue
Wed
Thu
Fri
Sat
Sun
Weekdays(2) = “Wed”
Element
Weekdays(4) = “Fri”
Subscript
BACS 287
Arrays




Arrays are useful when you want to store several
similar data values and do not want to create a
unique name for each one.
Arrays store “strongly typed” data, meaning that you
declare the datatype and that is the only type of data
that it can hold.
They are also useful because you can iterate
through each element in the array with a loop
construct.
Some arrays have more than 1 dimension.
BACS 287
Two-Dimension Arrays
Dim Homework (1, 6) as Integer
Columns
(0,0)
(0,1)
(0,2)
(0,3)
98
63
88
97
(1,0)
(1,1)
(1,2)
(1,3)
74
93
74
61
(0,4)
(0,5)
(0,6)
81
68
(1,4)
(1,5)
(1,6)
0
0
0
100
Rows
x = Homework(1,3)
Homework (row, col )
Value of x is 61
BACS 287
Array Definition

One-dimension arrays are called vectors.
When an array has 2 or more dimensions, it
is called a matrix.

You can define matrixes with many
dimensions in Visual Basic. It is unwise to
define a matrix with more than 3 or 4
dimensions.

If you do not give your array a data type, it is
type object by default.
BACS 287
Array Definition


Array elements can hold any valid data type
(including user-defined data structures,
objects, and other arrays).
You can dynamically change the number of
elements in an array with the REDIM
statement.
Dim intDynamicArray() as integer
ReDim intDynamicArray(x + 3)
BACS 287
Array Definition

You can declare arrays using all the valid scope
modifiers (Public, Private, Dim, …). The
following are valid.
Dim strName (10) as string
Public intCount(14) as integer
Static datDates(12, 11)as date
Dim intMatrix(3, 9, 20)as integer
Public dblArray() as double
BACS 287
Array Use

Once defined, arrays can be used in the
same way as variables.
intX = intArray(2)
strX = strArray(3,4)
strArray(1,2) = “A string”
...assume Row = 2 and Col = 5...
intX = intArray(Row, Col)
intY = intArray(Row + 1,Col + 2)
BACS 287
Array Use


Arrays can be assigned a value at the same
time that they are declared.
When you do this, you use a special syntax
that does not give a number of elements and
sets the array equal to a list in { } .
Dim bytX () as byte = {1, 4, 50, 25}
Dim intY (,) as integer = {{1,2}{2,4}{14,34}}
Normal
Parenthesis
Curley
Brackets
BACS 287
Collections




A collection is a group of individual objects treated
as one unit.
Each object in the collection has a unique index
assigned to it (starting with 0).
While this is similar to an array, it is actually more
flexible because the objects do not have to be of the
same type (i.e., not strongly typed).
Collections automatically resize when they need
more room.
BACS 287
Collection Classes

There are several built in collection classes
in Visual Basic. 3 commons ones are:

ArrayList – Similar to a standard array, but it can hold any
object and automatically resizes.

Stack – Data structure where items are added and removed
from the same side (LIFO).

Queue – Data structure where new items are added and old
ones removed from different ends (FIFO).
BACS 287
Collection Example
Dim MyArrayList as New ArrayList
MyArrayList.Add(“Fred”)
MyArrayList.Add(5)
MyArrayList.Add(#01/03/2007#)
. . .
For Each str as String in MyArrayList
Console.WriteLine(str)
Next str
BACS 287
Generics




Generics are a way to store strongly typed
collections. So, they are a special type of collection.
Remember, “strongly typed” means that when the
variable is defined, it can only be assigned certain
types of values (e.g., integer).
An advantage of a generic over an array is that it
automatically resizes when it is full and needs more
space.
The advantage of a generic over a collection is that it
produces more efficient (and less error prone) code
because it is strongly typed.
BACS 287
Generics Example
Dim MyStringList as New List(Of String)
MyStringList.Add(“a”)
MyStringList.Add(“b”)
MyStringList.Add(5)
Compiler error
. . .
For Each str as String in MyStringList
Console.WriteLine(str)
Next str
BACS 287
Array Practice 1



Create a 1 dimensional string array that
holds 27 elements.
Create a 2 dimensional integer array with 5
rows and 4 columns.
Create a 1 dimensional dynamic array to
hold double values
BACS 287
Practice 1 Answers
Dim strArray(26) as string
Dim int2DArray(4,3) as integer
Dim dblDynamic() as double
BACS 287
Array Practice 2

Create a 2 dimensional integer array with 3
rows and 4 columns. Use a FOR-NEXT loop
to load it with the numbers 1 to 12.
BACS 287
Practice 2 Answer
Dim intArray(2,3) as integer
Dim intCnt as integer = 1
For row as integer = 0 to 2
For col as integer = 0 to 3
intArray(row, col) = intCnt
intCnt += 1
Next col
Next row
BACS 287
Array Practice 3

Create a 2 dimensional integer array with 3
rows and 4 columns. Use a FOR-EACHNEXT loop to load it with the numbers 1 to
12.
BACS 287
Practice 3 Answer
Dim intArray(2,3) as integer
Dim intCnt as integer = 1
For each element as integer in intArray
element = intCnt
intCnt += 1
Next element
BACS 287