10/5/2008 Chapter 8 Objectives

10/5/2008
Objectives
Applied
• Given the specifications for an application that requires the use of
a one-dimensional, rectangular, or jagged array, write the code
that works with the array.
Chapter 8
• Given the specifications for an application that requires the use of
one of the collection classes presented in this chapter, write the
code that works with the collection.
collection
How to work with
arrays and collections
Knowledge
• Distinguish between a For loop and a For Each loop.
• Explain how the Array class can be used with an array.
• Distinguish between an untyped and a typed collection class.
• Describe the differences between these collection classes: list,
sorted list, queue, and stack.
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 1
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 2
How to declare and initialize an array
The syntax for declaring and initializing a
one-dimensional array
Declaring and initializing an array
Dim numbers(99) As Integer
Declaring and initializing a one-dimensional array
' provides for 100 numbers
Getting the upper bound from a variable
Dim arrayName(upperbound) As type
Declaring but not initializing a one-dimensional array
Dim maxTotals As Integer = CInt(txtUpperBound.Text)
Dim totals(maxTotals) As Decimal
Dim arrayName() As type
Using the New keyword to initialize an array
Initializing an array that has only been declared
Dim maxTotals As Integer = CInt(txtUpperBound.Text)
Dim totals() As Decimal
totals = New Decimal(maxTotals) {}
arrayName = New type(upperbound) {}
How to document the lower bound when you
declare an array
Dim titles(0 To 49) As String
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 3
Murach’s Visual Basic 2008, C8
' provides for 50 titles
© 2008, Mike Murach & Associates, Inc.
Slide 4
The syntax for referring to an element of an array
arrayName(index)
Default values for array elements
Data type
All numeric types
Char
Boolean
Date
All reference types
Code that assigns values to an array of Decimal
types
Default value
0 (zero)
Null (binary 0)
False
01/01/0001 00:00:00
Null reference
Dim totals(3) As Decimal
' this provides for 4 elements
totals(0) = 14.95D
totals(1) = 12.95D
totals(2) = 11.95D
totals(3) = 9.95D
'totals(4) = 8.95D
' this would throw an IndexOutOfRangeException
Code that assigns objects to an array of strings
Dim names(2) As
names(0) = "Ted
names(1) = "Sue
names(2) = "Ray
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 5
Murach’s Visual Basic 2008, C8
String
Lewis"
Jones"
Thomas"
© 2008, Mike Murach & Associates, Inc.
Slide 6
1
10/5/2008
The syntax for creating an array and assigning
values in one statement
The syntax for using the Length property of an
array
Dim arrayName() As type =
[New type(upperbound)] {value1[, value2]
[, value3]...}
arrayName.Length
A For loop that puts the numbers 0 through 9 into
an array
Examples that create an array and assign values
in one statement
Dim totals() As Decimal = {14.95D, 12.95D, 11.95D, 9.95D}
Dim names() as String =
{"Ted Lewis", "Sue Jones", "Ray Thomas"}
Dim numbers(9) As Integer
For i As Integer = 0 To numbers.Length - 1
numbers(i)
( ) = i
Next
Dim totals() As Decimal =
New Decimal(3) {14.95D, 12.95D, 11.95D, 9.95D}
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 7
Code that displays the numbers array in a
message box
Slide 8
Dim totals() As Decimal = {14.95D, 12.95D, 11.95D, 9.95D}
Dim sum As Decimal = 0
For i As Integer = 0 To totals.Length - 1
sum += totals(i)
Next
Dim average As Decimal = sum / totals.Length
The message box that’s displayed
© 2008, Mike Murach & Associates, Inc.
© 2008, Mike Murach & Associates, Inc.
Code that uses a For loop to compute the average
of a totals array
Dim numbersString As String = ""
For i As Integer = 0 To numbers.Length - 1
numbersString &= numbers(i).ToString & " "
Next
MessageBox.Show(numbersString, "Numbers Test")
Murach’s Visual Basic 2008, C8
Murach’s Visual Basic 2008, C8
Slide 9
Code that displays the totals array in a message
box
Dim totalsString As String = ""
For i As Integer = 0 To totals.Length - 1
totalsString &= total.ToString & vbCrLf
Next
MessageBox.Show("The totals are:" _
& vbCrLf & totalsString & vbCrLf & _
"Sum: " & sum & vbCrLf & "Average: " & average, _
"Totals Test")
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 10
The syntax of a For Each loop
For Each elementName [As type] In arrayName
statements
Next
Code that displays the numbers array in a
message box
Dim numbersString As String = ""
For Each number As Integer In numbers
numbersString &= number.ToString & " "
Next
MessageBox.Show(numbersString, "Numbers Test")
The message box that’s displayed
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 11
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 12
2
10/5/2008
Code that displays the totals array in a message
box
Code that computes the average of the totals
array
Dim totalsString As String = ""
For Each total In totals
totalsString &= total.ToString & vbCrLf
Next
MessageBox.Show("The totals are:" _
& vbCrLf & totalsString & vbCrLf & _
"Sum: " & sum & vbCrLf & "Average: " & average, _
"Totals Test")
Dim sum As Decimal = 0
For Each total As Decimal In totals
sum += total
Next
Dim average As Decimal = sum / totals.Length
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 13
The syntax for declaring a rectangular array
Dim arrayName(firstupperbound, secondupperbound) As type
Murach’s Visual Basic 2008, C8
Dim numbers(2,1) As Integer
Slide 14
The index values for the elements of a 3x2
rectangular array
0,0
1,0
2,0
A statement that creates a 3x2 array
© 2008, Mike Murach & Associates, Inc.
0,1
1,1
2,1
Code that assigns values to the numbers array
The syntax for referring to an element of a
rectangular array
numbers(0,0)
numbers(0,1)
numbers(1,0)
numbers(1,1)
numbers(2,0)
numbers(2,1)
arrayName(rowindex, columnindex)
=
=
=
=
=
=
1
2
3
4
5
6
Code that creates a 3x2 array and assigns values
with one statement
Dim numbers(,) As Integer = { {1,2}, {3,4}, {5,6} }
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 15
Slide 16
arrayName.GetLength(dimensionindex)
Dim products(,) As String = _
{{"VB08", "Murach's Visual Basic 2008"}, _
{"JSE6", "Murach's Java SE 6"}, _
{"A3VB", "Murach's ASP.NET 3.5 with VB 2008"}}
© 2008, Mike Murach & Associates, Inc.
© 2008, Mike Murach & Associates, Inc.
The syntax for using the GetLength method of a
rectangular array
Code that creates and assigns values to a 3x2
array of strings
Murach’s Visual Basic 2008, C8
Murach’s Visual Basic 2008, C8
Code that works with the numbers array
Dim
Dim
Dim
Dim
Slide 17
numbers(,) As Integer = { {1,2}, {3,4}, {5,6} }
numberOfRows As Integer = numbers.GetLength(0)
numberOfColumns As Integer = numbers.GetLength(1)
sumOfFirstRow As Integer = numbers(0,0) _
+ numbers(0,1)
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 18
3
10/5/2008
Code that displays the products array in a
message box
Code that displays the numbers array in a
message box
Dim productsString As String = ""
For i As Integer = 0 To products.GetLength(0) - 1
For j As Integer = 0 To products.GetLength(1) - 1
productsString &= products(i, j) & vbTab
Next j
productsString &= vbCrLf
Next i
MessageBox.Show(productsString, "Products Test")
Dim numbersString As String = ""
For i As Integer = 0 To numbers.GetLength(0) - 1
For j As Integer = 0 To numbers.GetLength(1) - 1
numbersString &= numbers(i, j) & " "
Next
numbersString &= vbCrLf
Next
MessageBox.Show(numbersString, "Numbers Test")
The message box that’s displayed
The message box that’s displayed
Murach’s Visual Basic 2008, C8
Slide 19
© 2008, Mike Murach & Associates, Inc.
Dim arrayName(numberofrows)() As type
arrayName(rowindex)(columnindex)
Code that declares a jagged array with three rows
Murach’s Visual Basic 2008, C8
'
'
'
'
3
3
4
2
rows are declared
columns for 1st row
columns for 2nd row
columns for 3rd row
© 2008, Mike Murach & Associates, Inc.
Slide 21
Dim numbers()() As Integer = {New Integer() {1, 2, 3}, _
New Integer() {4, 5, 6, 7}, _
New Integer() {8, 9}}
Code that creates a jagged array of strings
Dim titles()() As String = _
{N
{New
St
String(2)
i (2) {"W
{"War and
d P
Peace",
" "W
"Wuthering
th i
H
Heights",
i ht " _
"1984"}, _
New String(3) {"Casablanca", "Wizard of Oz", _
"Star Wars", "Birdy"}, _
New String(1) {"Blue Suede Shoes", "Yellow Submarine"}}
© 2008, Mike Murach & Associates, Inc.
Statements that assign values to the numbers
array
numbers(0)(0) = 1
numbers(0)(1) = 2
numbers(0)(2)
b
(0)(2) = 3
Murach’s Visual Basic 2008, C8
numbers(1)(0)
numbers(1)(1)
numbers(1)(2)
b
(1)(2)
numbers(1)(3)
=
=
=
=
4
5
6
7
numbers(2)(0) = 8
numbers(2)(1) = 9
© 2008, Mike Murach & Associates, Inc.
Slide 22
Code that displays the numbers array in a
message box
Code that creates the numbers array with one
statement
Murach’s Visual Basic 2008, C8
Slide 20
© 2008, Mike Murach & Associates, Inc.
The syntax for referring to an element of a jagged
array
The syntax for declaring a jagged array
Dim numbers (2)() As Integer
numbers(0) = New Integer(2) {}
numbers(1) = New Integer(3) {}
numbers(2) = New Integer(1) {}
Murach’s Visual Basic 2008, C8
Slide 23
Dim numbersString As String = ""
For i As Integer = 0 To numbers.GetLength(0) - 1
For j As Integer = 0 To numbers(i).Length - 1
numbersString &= numbers(i)(j) & " "
Next
numbersString &= vbCrLf
Next
MessageBox.Show(numbersString,
g
g "Jagged
gg
Numbers Test")
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 24
4
10/5/2008
Code that displays the titles array in a message
box
Common properties and methods of the Array
class
Dim numbersString As String = ""
For i As Integer = 0 To titles.GetLength(0) - 1
For j As Integer = 0 To titles(i).Length - 1
numbersString &= titles(i)(j) & "
"
Next j
numbersString &= vbCrLf
Next i
MessageBox.Show(numbersString,
g
g "Jagged
gg
Titles Test")
Property
Length
Instance method
GetLength(dimension)
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
GetUpperBound(dimension)
Slide 25
Common properties and methods of the Array
class (continued)
Static method
Copy(array1, array2, length)
BinarySearch(array, value)
Sort(array)
Murach’s Visual Basic 2008, C8
Description
Copies some or all of the values
in one array to another array.
Searches a one-dimensional array
that’s in ascending order for an
element
l
t with
ith a specified
ifi d value
l
and returns the index for that
element.
Sorts the elements in a onedimensional array into ascending
order.
© 2008, Mike Murach & Associates, Inc.
Slide 27
Code that uses the Sort method
Murach’s Visual Basic 2008, C8
Description
Gets the number of elements in all of
the dimensions of an array.
Description
Gets the number of elements in the
specified dimension of an array.
Gets the index of the last element in
the specified dimension of an array.
© 2008, Mike Murach & Associates, Inc.
Slide 26
Code that uses the GetLength and
GetUpperBound methods
Dim numbers() As Integer = {1, 2, 3, 4}
Dim length As Integer = numbers.GetLength(0)
' length = 4
Dim upperBound As Integer = numbers.GetUpperBound(0)
' upperBound = 3
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 28
Code that uses the BinarySearch method
Dim lastNames() As String = {"Boehm", "Taylor", _
"Murach", "Vasquez"}
Array.Sort(lastNames)
Dim message As String = ""
For Each lastName As String In lastNames
message &= lastName & vbCrLf
Next
MessageBox.Show(message, "Sorted Last Names")
Dim employees() As String = {"Adams", "Finkle", _
"Lewis", "Potter"}
Dim salesAmounts() As Decimal = {3275.68D, 4298.55D, _
5289.57D, 1933.98D}
Dim index As Integer = Array.BinarySearch(employees, _
"Finkle")
Dim salesAmount As Decimal = salesAmounts(index)
' salesAmount = 4298.55
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 29
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 30
5
10/5/2008
Code that copies all the elements of an array
Code that creates a reference to another array
Dim inches1() As Double = {1, 2, 3}
Dim inches2() As Double = inches1
inches2(2) = 4
' inches1(2) also = 4
How to copy elements of one array to another array
The syntax for copying elements of an array
Array.Copy(fromarray, toarray, length)
Code that copies some of the elements of an array
Another way to copy elements from one array to another
Array.Copy(fromarray, fromindex, toarray, toindex, length)
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 31
A Function procedure that returns an array
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 32
A Sub procedure that converts an array of inches to an
array of centimeters
Private Function RateArray( _
ByVal elementCount As Integer) As Decimal()
Dim rates(elementCount - 1) As Decimal
For i As Integer = 0 To rates.Length - 1
rates(i) = (i + 1) / 100D
Next
Return rates
End Function
Private Sub ConvertToCentimeters( _
ByRef measurements() As Double)
For i As Integer = 0 To measurements.Length - 1
measurements(i) *= 2.54
Next
End Sub
A statement that calls the procedure
Statements that declare an array and call the procedure
Dim rates() As Decimal = Me.RateArray(4)
© 2008, Mike Murach & Associates, Inc.
Dim names() As String = {"Boehm", "Murach", "Vasquez"}
Dim lastTwoNames(1) As String
Array.Copy(names, 1, lastTwoNames, 0, 2)
How to code a procedure that accepts an array
argument
How to return an array from a procedure
Murach’s Visual Basic 2008, C8
Dim inches() As Double = {1, 2, 3, 4}
Dim centimeters(3) As Double
Array.Copy(inches, centimeters, inches.Length)
For i As Integer = 0 To centimeters.Length - 1
centimeters(i) *= 2.54
' set new values for the new array
Next
Dim measurements() As Double = {1, 2, 3}
Me.ConvertToCentimeters(measurements)
Slide 33
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 34
Commonly used collection classes
How arrays and collections are similar
• Both can store multiple elements, which can be value types or
reference types.
.NET 2.0 to 3.5
List()
.NET 1.x
ArrayList
SortedList()
SortedList
Queue()
Queue
Stack()
Stack
How arrays and collections are different
• An array is a feature of the Visual Basic language that inherits the
Array class. Collections are other classes in the .NET Framework.
• Collection classes provide methods to perform operations that
arrays don’t provide.
• Arrays are fixed in size. Collections are variable in size.
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 35
Murach’s Visual Basic 2008, C8
Description
Uses an index to access each
element. Efficient for accessing
elements sequentially; inefficient
for inserting elements into a list.
Uses a key to access a value, which
can be any type of object. Efficient
for inserting elements into a list;
can be inefficient for accessing
elements sequentially.
Uses special methods to add and
remove elements.
Uses special methods to add and
remove elements.
© 2008, Mike Murach & Associates, Inc.
Slide 36
6
10/5/2008
Untyped collections: An example
Typed collections: An example
Dim numbers As New ArrayList
numbers.Add(3)
numbers.Add(70)
numbers.Add("Test")
' will compile, but causes an exception
Dim sum As Integer = 0
Dim number As Integer
For i As Integer = 0 To numbers.Count - 1
' cast is required
number = CInt(numbers(i))
sum += number
Next
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 37
A statement that creates a list of String elements
Dim titles As New List(Of String)
Dim numbers As New List(Of Integer)
numbers.Add(3)
numbers.Add(70)
'numbers("Test")
' won't compile - prevents runtime error
Dim sum As Integer = 0
Dim number As Integer
For i As Integer = 0 To numbers.Count - 1
' no cast is required
number = numbers(i)
sum += number
Next
Murach’s Visual Basic 2008, C8
Item(index)
Dim prices As New List(Of Decimal)
A statement that creates a list of strings with a
capacity of 3
Capacity
p
y
Dim lastNames As New List(Of String)(3)
Count
Method
Add(object)
Clear()
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 39
Common properties and methods of the List()
class (continued)
Method
Contains(object)
Insert(index, object)
Remove(object)
RemoveAt(index)
BinarySearch(object)
Sort()
Murach’s Visual Basic 2008, C8
Description
Returns a Boolean value that indicates if
the list contains the specified object.
Inserts an element into a list at the
specified index.
Removes the first occurrence of the
specified object from the list.
Removes the element at the specified
index of a list.
Searches a list for a specified object and
returns the index for that object.
Sorts the elements in a list into ascending
order.
© 2008, Mike Murach & Associates, Inc.
Slide 41
Slide 38
Common properties and methods of the List()
class
Property
A statement that creates a list of Decimal
elements
© 2008, Mike Murach & Associates, Inc.
Murach’s Visual Basic 2008, C8
Description
Gets or sets the element at the specified index.
The index for the first item in a list is 0. Since
Item is the default property, its name can be
omitted.
Gets or sets the number of elements the list
can hold.
Gets the number of elements in the list.
Description
Adds an element to the end of a list and
returns the element’s index.
Removes all elements from the list and sets its
Count property to zero.
© 2008, Mike Murach & Associates, Inc.
Slide 40
Code that causes the size of the list of names to be
increased
Dim lastNames As New List(Of String)(3)
' capacity is 3 elements
lastNames.Add("Boehm")
lastNames.Add("Prince")
lastNames.Add("Murach")
lastNames.Add("Taylor")
' capacity is doubled to 6 elements
lastNames Add("Vasquez")
lastNames.Add(
Vasquez )
lastNames.Add("Steelman")
lastNames.Add("Slivkoff")
' capacity is doubled to 12 elements
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 42
7
10/5/2008
Code that retrieves the first value from the list
The syntax for retrieving a value from a list
Dim sales1 As Decimal = salesTotals(0)
listName[.Item](index)
Code that creates a list that holds Decimal values
Dim newSalesTotals() As Decimal = {3275.68D, 4398.55D, _
5289.75D, 1933.98D}
Dim salesTotals As New List(Of Decimal)
For Each d As Decimal In newSalesTotals
salesTotals.Add(d)
Next
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 43
Code that displays the list in a message box
' sales1 = 3275.68
Code that inserts and removes an element from the
list
salesTotals.Insert(0, 2745.73D)
' insert a new 1st
sales1 = salesTotals(0)
' sales1 =
Dim sales2 As Decimal = salesTotals(1)
' sales2 =
salesTotals.RemoveAt(1)
' remove the 2nd
sales2 = salesTotals(1)
' sales2 =
Murach’s Visual Basic 2008, C8
element
2745.73
3275.68
element
4398.55
© 2008, Mike Murach & Associates, Inc.
Slide 44
Code that checks for an element in the list and
removes it if it exists
Dim salesTotalsString As String = ""
For Each d As Decimal In salesTotals
salesTotalsString &= d.ToString & vbCrLf
Next
MessageBox.Show(salesTotalsString, "Sales Totals")
Dim x As Decimal = 2745.73D
If salesTotals.Contains(x) Then
salesTotals.Remove(x)
End If
The message box that’s displayed
Code that sorts and searches the list
salesTotals.Sort()
Dim sales2Index As Integer = _
salesTotals.BinarySearch(sales2)
' sales2Index = 1
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 45
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 46
Common methods of the SortedList() class
Common properties of the SortedList() class
Property
Item(key)
Keys
Values
Capacity
Count
Murach’s Visual Basic 2008, C8
Method
Description
Gets or sets the value of the element with the specified
key. Since Item is the default property, its name can
be omitted.
Gets a collection that contains the keys in the list.
Gets a collection that contains the values in the list.
Gets or sets the number of elements the list can hold.
Gives the number of elements in the list.
© 2008, Mike Murach & Associates, Inc.
Slide 47
Add(key, value)
Clear()
ContainsKey(key)
ContainsValue(value)
Murach’s Visual Basic 2008, C8
Description
Adds an element with the specified key
and value to the sorted list.
Removes all elements from the sorted
list.
Returns a Boolean value that indicates
whether
w
e e or
o not
o thee sorted
so ed list
s contains
co
s
the specified key.
Returns a Boolean value that indicates
whether or not the sorted list contains
the specified value.
© 2008, Mike Murach & Associates, Inc.
Slide 48
8
10/5/2008
Common methods of the SortedList() class (cont.)
Properties of the KeyValuePair structure
Method
Property
Remove(key)
RemoveAt(index)
Description
Removes the element with the specified
key from the sorted list.
Removes the element at the specified
index from the sorted list.
Key
Value
Description
The key for the SortedList item.
The value associated with the key.
Code that creates and loads a sorted list
Dim salesList As New SortedList(Of String, Decimal)
salesList.Add("Adams", 3274.68D)
salesList.Add("Finkle", 4398.55D)
salesList.Add("Lewis", 5289.75D)
salesList.Add("Potter", 1933.97D)
Code that looks up a value in the sorted list based
on a key
Dim employeeKey As String = "Lewis"
Dim salesTotal As Decimal = salesList(employeeKey)
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 49
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 50
Properties and methods of the Queue() class
Code that converts the sorted list to a tabdelimited string
Property
Count
Dim salesTableString As String = ""
For Each employeeSalesEntry _
As KeyValuePair(Of String, Decimal) In salesList
salesTableString &= employeeSalesEntry.Key _
& vbTab & _
employeeSalesEntry.Value & vbCrLf
N t
Next
MessageBox.Show(salesTableString, "Sorted List Totals")
Method
Enqueue(object)
Dequeue()
Clear()
Peek()
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 51
Code that uses a queue
Murach’s Visual Basic 2008, C8
Property
Count
Method
Push(object)
Pop()
The message that’s displayed
Clear()
Peek()
© 2008, Mike Murach & Associates, Inc.
© 2008, Mike Murach & Associates, Inc.
Slide 52
Properties and methods of the Stack() class
Dim nameQueue As New Queue(Of String)
nameQueue.Enqueue("Boehm")
nameQueue.Enqueue("Murach")
nameQueue.Enqueue("Vasquez")
Dim nameQueueString As String = ""
Do While nameQueue.Count > 0
nameQueueString &= nameQueue.Dequeue & vbCrLf
Loop
MessageBox.Show(nameQueueString, "Queue")
Murach’s Visual Basic 2008, C8
Description
Gets the number of items in the queue.
Description
Adds the specified object to the end of the
queue.
Gets the object at the front of the queue and
removes it from the queue.
Removes all items from the queue.
Retrieves the next item in the queue without
deleting it.
Slide 53
Murach’s Visual Basic 2008, C8
Description
Gets the number of items in the stack.
Description
Adds the specified object to the top of the stack.
Gets the object at the top of the stack and removes
i from
it
f
the
h stack.
k
Removes all items from the stack.
Retrieves the next item in the stack without
deleting it.
© 2008, Mike Murach & Associates, Inc.
Slide 54
9
10/5/2008
Code that uses a stack
Code that creates an array list that holds decimal
values
Dim nameStack As New Stack(Of String)
nameStack.Push("Boehm")
nameStack.Push("Murach")
nameStack.Push("Vasquez")
Dim nameStackString As String = ""
Do While nameStack.Count > 0
nameStackString &= nameStack.Pop & vbCrLf
Loop
MessageBox.Show(nameStackString, "Stack")
Dim newSalesTotals() As Decimal = {3275.68D, 4398.55D, _
5289.75D, 1933.98D}
Dim salesTotals As New ArrayList
For Each d As Decimal In newSalesTotals
salesTotals.Add(d)
Next
Code that retrieves the first value from the array
list
Th message th
The
that’s
t’ displayed
di l
d
Dim sales1 As Decimal = CDec(salesTotals(0))
' sales1 = 3275.68
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 55
salesTotals.Insert(0, 2745.73D)
' insert a new 1st element
sales1 = CDec(salesTotals(0))
' sales1 = 2745.73
Dim sales2 As Decimal = CDec(salesTotals(1))
' sales2 = 3275.68
salesTotals.RemoveAt(1)
' remove the 2nd element
sales2 = CDec(salesTotals(1))
' sales2 = 4398.55
© 2008, Mike Murach & Associates, Inc.
© 2008, Mike Murach & Associates, Inc.
Slide 56
Code that displays the array list in a message box
Code that inserts and removes an element from
the array list
Murach’s Visual Basic 2008, C8
Murach’s Visual Basic 2008, C8
Slide 57
Dim salesTotalsString As String = ""
For Each d As Decimal In salesTotals
salesTotalsString &= d.ToString & vbCrLf
Next
MessageBox.Show(salesTotalsString, "Sales Totals")
The message box that’s displayed
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 58
Code that checks for an element in the array list
and removes it if it exists
Dim x As Decimal = 2745.73D
If salesTotals.Contains(x) Then
salesTotals.Remove(x)
End If
Code that sorts and searches the array list
salesTotals Sort()
salesTotals.Sort()
Dim sales2Index As Integer = _
salesTotals.BinarySearch(sales2)
Murach’s Visual Basic 2008, C8
© 2008, Mike Murach & Associates, Inc.
Slide 59
10