download

CHAPTER TEN
Working with Arrays and
Collections
10- 2
Introduction
• An array is a variable with a single symbolic
name that represents many different data items.
• Visual Basic .NET provides a number of classes
that manage collections of objects.
• A collection of objects is like an array in that one
collection is associated with many objects.
• This chapter covers the ArrayList, Hashtable,
and SortedList collections.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 3
Objectives
• Explain why arrays are needed to solve many
types of problems.
• Construct an array to store multiple related data
values.
• Use ArrayList, Hashtable, and SortedList
collections to store and process data values.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 4
10.1 Solving Problems with Arrays
• Simple variable means a variable that can store
only one value.
• The Problem
– Calculate the rate of return for 10 different
companies and those companies who exceeded
the average of the group.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 5
10.1 Solving Problems with Arrays (cont.)
• The Solution Using Simple Variables
– Write the pseudocode for the problem solution.
• Two passes through the data:
– One to compute the average.
– Two to determine who exceeds the average.
– Not a good way to solve the problem.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 6
10.1 Solving Problems with Arrays (cont.)
• The Structure of an Array
– Each element of an array is like a simple variable.
– Starting at zero, the elements in an array are
numbered.
– The element number is called the subscript.
– Another name for the element number is index
value.
– Arrays are also called subscripted or indexed
variables.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 7
10.1 Solving Problems with Arrays (cont.)
• In a one-dimensional array, each element is
referenced using a single subscript value.
• Higher-dimensional arrays are called
multidimensional arrays.
• Syntax for a one-dimensional array:
– ArrayName(SubscriptValue)
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 8
10.1 Solving Problems with Arrays (cont.)
• The Solution Using Arrays
– An array will provide a better problem solution.
– Use a numeric variable for a subscript.
– The subscript begins at zero.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 9
10.1 Solving Problems with Arrays (cont.)
• Storing Data in Arrays versus Databases
–
–
–
–
–
Reading data from a database is an option.
Arrays are variables that are stored in RAM.
Data stored in RAM can be accessed quickly.
Storage for arrays in RAM is valuable space.
Database are stored on disk and retrieval is
relatively slow.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 10
10.1 Solving Problems with Arrays (cont.)
• Factors to consider for array versus database
are:
1. Execution speed.
2. Volume of data.
3. Clarity of the code that accomplishes the task.
• As RAM gets less expensive, the distinction
between databases and arrays gets less clear.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 11
10.1 Solving Problems with Arrays (cont.)
• Multidimensional Arrays
– The number of dimensions in an array is known
as dimensionality.
– A two-dimensional array uses two subscripts.
• It is also referred to as a matrix or table.
• Syntax: ArrayName(RowSubscriptValue,
ColumnSubscriptValue).
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 12
10.2 Declaring Arrays
• Arrays can store different data types just as
simple variables can.
• General syntax of the Dim statement:
– Dim ArrayName(subscripts) As Type
• Subscript bounds are specified by the Dim
statement.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 13
10.2 Declaring Arrays (cont.)
• Complete the examples listed below in your
textbook:
– Example 10.1 Populating an Array from a
Database.
– Example 10.2 Sequentially Searching an
Unordered Array.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 14
10.3 The ArrayList Collection
• The ArrayList collection class is like a onedimensional array.
• It is indexed using a zero-based indexing
system.
• Has a dynamic resizing ability.
• Its capacity automatically grows by adding items.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 15
10.3 The ArrayList Collection (cont.)
– Properties
• Capacity
• Count
• Item
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 16
10.3 The ArrayList Collection (cont.)
– Methods
Add
BinarySearch
Clear
Contains
IndexOf
Insert
McGraw Hill/Irwin
Remove
RemoveAt
Reverse
Sort
ToArray
TrimToSize
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 17
10.3 The ArrayList Collection (cont.)
• Complete the examples listed below in your
textbook:
– Example 10.3 Populating an ArrayList from a
Database.
– Example 10.4 Performing a Binary Search.
– Example 10.5 Performing Multiple Result
Search.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 18
10.4 The Hashtable Collection
• A Hashtable is a data structure that supports
very fast access to information based on a key
field.
• The hash function takes a unique key field and
transforms it into a new value called a bucket
number.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 19
10.4 The Hashtable Collection (cont.)
– Important Points about Hashtables:
• Support very fast access.
• Store the records randomly.
• Sequential access to the entire set of records is
difficult.
• Exact key must be used for access.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 20
10.4 The Hashtable Collection (cont.)
– Properties
•
•
•
•
Count
Item
Keys
Values
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 21
10.4 The Hashtable Collection (cont.)
– Methods
•
•
•
•
•
•
•
Add
Clear
Contains
ContainsKey
ContainsValue
GetEnumerator
Remove
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 22
10.4 The Hashtable Collection (cont.)
• Complete the examples listed below in your
textbook:
– Example 10.6 Populating a Hashtable from a
Database Table.
– Example 10.7 Searching a Hashtable.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 23
10.5 The SortedList Collection
• A SortList is a hybrid between a Hashtable and
an ArrayList.
• A SortedList maintains an array for the keys and
another array for the associated values.
• The elements of a SortedList are ordered by the
value of the keys.
• Operations on a SortedList tend to be slower
than operations on a Hashtable.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 24
10.5 The SortedList Collection (cont.)
– Properties
•
•
•
•
Count
Item
Keys
Values
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 25
10.5 The SortedList Collection (cont.)
– Methods
Add
Clear
ContainsKey
ContainsValue
GetByIndex
GetEnumerator
GetKey
McGraw Hill/Irwin
IndexOfKey
IndexOfValue
Remove
RemoveAt
SetByIndex
TrimToSize
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 26
10.5 The SortedList Collection (cont.)
• Complete the examples listed below in your
textbook:
– Example 10.8 Populating a SortedList from a
Database Table.
– Example 10.9 Finding Average Rate of Return.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 27
Chapter Summary
• An array is a variable that stores more than a
single value.
• Arrays must be declared using a Dim, Static, or
Public statement.
• Arrays also need to be dimensioned.
• To access an element of an array, the
programmer specifies both the array name and
specific subscript values.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 28
Chapter Summary (cont.)
• The declaration statement that creates an array
initializes all of its elements to zero or the zerolength string.
• We start by populating the array.
• Then, we can use the array to process data.
• An ArrayList collection is an index-based data
structure like an array.
• An ArrayList does not have a set capacity.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
10- 29
Chapter Summary (cont.)
• A Hashtable collection is a key-based data
structure.
• Unlike an array, a Hashtable collection uses a
key field to store and retrieve elements.
• Hashing has a randomizing effect.
• A SortedList provides both index-based and keybased access to its elements.
• A SortedList shares the capabilities of the
ArrayList and Hashtable.
McGraw Hill/Irwin
©2002 by The McGraw-Hill Companies, Inc. All rights reserved.