After studying this chapter you should be able to 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.
Trang 1Collections
Trang 2• 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
Trang 3• 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
Trang 410.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.
Trang 5• 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.
Trang 610.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.
Trang 7• 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)
Trang 810.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.
Trang 9• 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.
Trang 1010.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
Trang 11• 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).
Trang 1210.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
Trang 13• 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.
Trang 1410.3 The ArrayList Collection
• The ArrayList collection class is like a
one-dimensional array
• It is indexed using a zero-based indexing
system
• Has a dynamic resizing ability
• Its capacity automatically grows by adding items
Trang 15– Properties
• Capacity
• Count
• Item
Trang 1610.3 The ArrayList Collection (cont.)
Trang 17– Example 10.3 Populating an ArrayList from a Database.
– Example 10.4 Performing a Binary Search.
– Example 10.5 Performing Multiple Result Search.
Trang 1810.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
Trang 19– 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
Trang 2010.4 The Hashtable Collection (cont.)
Trang 2210.4 The Hashtable Collection (cont.)
• Complete the examples listed below in your
Trang 23an 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
Trang 2410.5 The SortedList Collection (cont.)
Trang 2610.5 The SortedList Collection (cont.)
• Complete the examples listed below in your
Trang 27single 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
Trang 28Chapter Summary (cont.)
• The declaration statement that creates an array
initializes all of its elements to zero or the
zero-length 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
Trang 29• 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
key-based access to its elements