Tải bản đầy đủ (.ppt) (29 trang)

Lecture Learning programming using Visual Basic Net – Chapter 10 Working with arrays and collections

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (79.54 KB, 29 trang )

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.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.

1010


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.

1011


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.

1012


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.

1013


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.

1014


10.3 The ArrayList Collection (cont.)
– Properties

• Capacity
• Count

• Item

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

1015


10.3 The ArrayList Collection (cont.)
– Methods

Add
Remove
BinarySearchRemoveAt
Clear
Reverse
Contains
Sort
IndexOf
ToArray
Insert
TrimToSize

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


1016


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.

1017


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.

1018


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.

1019


10.4 The Hashtable Collection (cont.)
– Properties







Count
Item
Keys
Values

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

1020


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.

1021


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.

1022


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.

1023


10.5 The SortedList Collection (cont.)
– Properties






Count
Item
Keys
Values

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All

rights reserved.

1024


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.

1025



×