CHAPTER TEN Working with Arrays and Collections – Pemrograman Visual Matakuliah

advertisement
Matakuliah
Tahun
: T0063 – Pemrograman Visual
: 2009
Working with Arrays and Collections
CHAPTER TEN
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.
Bina Nusantara University
2
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.
Bina Nusantara University
3
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.
Bina Nusantara University
4
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.
Bina Nusantara University
5
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.
Bina Nusantara University
6
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)
Bina Nusantara University
7
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.
Bina Nusantara University
8
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.
Bina Nusantara University
9
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.
Bina Nusantara University
10
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).
Bina Nusantara University
11
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.
Bina Nusantara University
12
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.
Bina Nusantara University
13
10.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.
Bina Nusantara University
14
10.3 The ArrayList Collection (cont.)
– Properties
• Capacity
• Count
• Item
Bina Nusantara University
15
10.3 The ArrayList Collection (cont.)
– Methods
• Add
• BinarySearch
• Clear
• Contains
• IndexOf
• Insert
Bina Nusantara University
Remove
RemoveAt
Reverse
Sort
ToArray
TrimToSize
16
Download