Вы находитесь на странице: 1из 58

ARRAYS

PREVIEWING THE TREASURES GIFT SHOP


APPLICATION

Treasures application

Allows the user to enter a product ID


Displays the products price

Open the Treasures.exe file

PREVIEWING THE TREASURES GIFT SHOP


APPLICATION (CONTD.)

Figure 9-1 Interface showing the products price


3

LESSON A OBJECTIVES
After studying Lesson A, you should be able to:
Declare and initialize a one-dimensional array
Store data in a one-dimensional array
Determine the number of array elements and the
highest subscript
Traverse a one-dimensional array
Code a loop using the For EachNext statement

LESSON A OBJECTIVES (CONTD.)


Compute the total and average of a onedimensional arrays contents
Find the highest value in a one-dimensional
array
Associate a list box with a one-dimensional array
Use a one-dimensional array as an accumulator
or a counter
Sort a one-dimensional array

USING ARRAYS

Simple (scalar) variable

Array

One that is unrelated to any other variable in


memory
Group of related variables
Variables have same name and data type

Reasons to use arrays

Simplifies process of coding application


Increases run-time efficiency of program

ONE-DIMENSIONAL ARRAYS

One-dimensional array

Subscript

Variables stored in consecutive memory locations


Visualized as a column of variables
Indicates variables position in the array
Starts at 0 for first array variable

Refer to array variable by array name and


subscript

Example: strCities(0) is first variable in


strCities array
7

ONE-DIMENSIONAL ARRAYS (CONTD.)

Figure 9-2 Illustration of the one-dimensional strCities array


8

Figure 9-3 Syntax versions and examples of declaring a one-dimensional array

ONE-DIMENSIONAL ARRAYS (CONTD.)


Element: Refers to individual array variable
Arrays are initialized by computer when created

Arrays of numeric variables are initialized to 0


Arrays of string variables are initialized using
keyword Nothing

Arrays of Boolean variables are initialized to False


Arrays of Date variables are initialized to 12:00 AM
January 1, 0001

Populating the array

Assigning initial values


10

ONE-DIMENSIONAL ARRAYS (CONTD.)


After array is declared, you can store data in
array
To enter data into array:

Use assignment statement


Use TryParse statement

Example syntax using assignment statement


arrayname(subscript) = value

11

Figure 9-4 Examples of statements used to store data in a one-dimensional array 12

DETERMINING THE NUMBER OF


ELEMENTS AND THE HIGHEST SUBSCRIPT

Arrays Length property

Stores number of elements in array

Highest subscript in a one-dimensional array

Number is an integer

Subtract 1 from arrays Length property

GetUpperBound method

Returns an integer that represents highest subscript


in specified array dimension

For one-dimensional array, specified dimension is 0

13

DETERMINING THE NUMBER OF ELEMENTS


AND THE HIGHEST SUBSCRIPT (CONTD.)

Figure 9-5 Syntax and an example of a one-dimensional arrays Length property


14

DETERMINING THE NUMBER OF ELEMENTS


AND THE HIGHEST SUBSCRIPT (CONTD.)

Figure 9-6 Syntax and an example of a one-dimensional arrays GetUpperBound method


15

TRAVERSING A ONE-DIMENSIONAL ARRAY

Traversing an array

Look at each array element, one by one


Beginning with first element
Ending with last element

Figure 9-7 shows two examples

16

TRAVERSING A ONE-DIMENSIONAL ARRAY


(CONTD.)

Figure 9-7 Examples of loops used to traverse a one-dimensional array


17

THE FOR EACHNEXT STATEMENT

For EachNext statement

Unlike ForNext statement:

Used to process each element in array

You do not have to keep track of array subscripts


Can only read array values, not permanently modify
them

Declare variable within For EachNext


statement to refer to each array element, one at a
time

18

Figure 9-9 Syntax and an example of the For EachNext statement

19

CALCULATING THE TOTAL AND AVERAGE


VALUES

Sweet Tooth Chocolate application

Displays total number of pounds of chocolate sold


during six month period
Displays average number of pounds sold each month

btnCalc controls Click event procedure

Adds array element values


Divides total by number of array elements
Displays average amount on form

20

Figure 9-11
Examples of code for the
btnCalc_Click event procedure
(continues)

21

Figure 9-11
Examples of code for the btnCalc_Click event procedure (contd.)
22

FINDING THE HIGHEST VALUE

Cycles Galore application

Displays highest bonus amount earned during the


month
Displays number of salespeople who earned that
amount

Get Highest Bonus buttons Click event


procedure

Code shown in Figure 9-16

23

Figure 9-16 Get Highest Bonus buttons Click event procedure

24

ARRAYS AND COLLECTIONS


Items in a list box belong to a collection
Collections and arrays

Group of individual objects treated as one unit


Each object identified by unique number
First index in a collection and first array subscript
are both 0

List boxes can be associated with arrays

25

ARRAYS AND COLLECTIONS (CONTD.)

Prairie Auditorium application

Figure 9-17 Problem specification for the Prairie Auditorium application

26

ARRAYS AND COLLECTIONS (CONTD.)

Figure 9-18 Illustration of the relationship between the list box and array

27

Figure 9-19 Most of the code for the Prairie Auditorium application
28

ACCUMULATOR AND COUNTER ARRAYS

One-dimensional arrays

Often used to either accumulate or count related


values

Warren School application

Uses an accumulator array


Allows user to enter amount of candy sold for each
student
Displays total number sold for each candy

29

Figure 9-24 btnAdd controls Click event procedure


30

SORTING A ONE-DIMENSIONAL ARRAY

Sorting

Array.Sort method

Arranging data in a specific order

Sorts elements of one-dimensional array in ascending


order
Syntax: Array.Sort(arrayname)

To sort array in descending order:

First use Array.Sort to sort in ascending order


Then use Array.Reverse to reverse order of array
elements
31

Figure 9-26 Syntax and examples of the Array.Sort and Array.Reverse methods

32

SORTING A ONE-DIMENSIONAL ARRAY


(CONTD.)

Figure 9-28 State names displayed in ascending order

33

LESSON A SUMMARY
An array groups variables with same data type
under one name
Individual array variables also called elements
Arrays may be declared with or without list of
initial values

Values can be assigned after declaration

Arrays Length property returns array size

Can also use GetUpperBound method

34

LESSON A SUMMARY (CONTD.)

To traverse each element in a one-dimensional


array

Use a loop coded with one of the following


statements:

DoLoop, ForNext, For EachNext

To associate list box with array:

Use list box items index and array elements


subscript

Array.Sort: Sorts elements in ascending order


Array.Reverse: Reverses order of array elements

35

LESSON B OBJECTIVES
After studying Lesson B, you should be able to:
Explain the relationship between the elements in
parallel one-dimensional arrays
Create parallel one-dimensional arrays
Locate information in two parallel onedimensional arrays

36

PARALLEL ONE-DIMENSIONAL ARRAYS

Parallel arrays

Two or more arrays whose elements are related by


their position in arrays (by their subscripts)
Arrays may be different data types

Scenario involving two parallel arrays

Parallel arrays named strIds and intPrices


Each strIds element corresponds to intPrices
element located in same position
Search strIds array for product ID
View corresponding element in intPrices array
37

PARALLEL ONE-DIMENSIONAL ARRAYS


(CONTD.)

Figure 9-31 Illustration of two parallel one-dimensional arrays


38

Figure 9-32 btnDisplay controls Click event procedure


using parallel one-dimensional arrays (continues)
39

Figure 9-32 btnDisplay controls Click event procedure


using parallel one-dimensional arrays (contd.)
40

PARALLEL ONE-DIMENSIONAL ARRAYS


(CONTD.)

Figure 9-33 Interface showing the price for product ID CR20


41

LESSON B SUMMARY

To create parallel one-dimensional arrays:

Create two or more one-dimensional arrays


Ensure value stored in first element in first array
corresponds to value stored in same element in other
arrays

42

LESSON C OBJECTIVES
After studying Lesson C, you should be able to:
Declare and initialize a two-dimensional array
Store data in a two-dimensional array
Sum the values in a two-dimensional array
Search a two-dimensional array

43

TWO-DIMENSIONAL ARRAYS

Two-dimensional array

Resembles a table
Storing variables (elements) in rows and columns

Identifying a two-dimensional array element

Use unique combination of two subscripts to specify


elements row and column position
Subscripts begin at 0
Example: strProducts(1,2)refers to second row, third
column

44

TWO-DIMENSIONAL ARRAYS (CONTD.)

Figure 9-37 Names of some of the elements in the strProducts array

45

Figure 9-38 Syntax versions and examples of declaring a two-dimensional array (continues)
46

Figure 9-38 Syntax versions and examples of declaring a two-dimensional array (contd.)

47

Figure 9-39
Examples of
statements used to
store data in a twodimensional array
(continues)

48

Figure 9-39 Examples of statements used to store data in a two-dimensional array (contd.)

49

Figure 9-40 Syntax and an example of a two-dimensional arrays GetUpperBound method


50

TRAVERSING A TWO-DIMENSIONAL ARRAY


One loop used to traverse one-dimensional array
Two loops used to traverse two-dimensional array

Outer loop and nested loop


One keeps track of row subscript
One keeps track of column subscript

Can also traverse two-dimensional array using:

One For EachNext loop


This method cannot permanently modify values

51

Figure 9-41 Examples of loops used to traverse a two-dimensional array (continues)


52

Figure 9-41 Examples of loops used to traverse a two-dimensional array (contd.) 53

SEARCHING A TWO-DIMENSIONAL ARRAY

Two-dimensional arrays versus parallel arrays

Both can represent data in tabular format


All data in two-dimensional array must be same type

New version of Treasures application

Use one two-dimensional array to store price list


2-D array replaces two parallel arrays in first version

54

Figure 9-45 btnDisplay controls Click event procedure using a


two-dimensional array (continues)
55

Figure 9-45 btnDisplay controls Click event procedure using a


two-dimensional array (contd.)
56

LESSON C SUMMARY

To declare a two-dimensional array:

Use either syntax:

To refer to two-dimensional array element:


Use syntax:
arrayName(rowSubscript, columnSubscript)

57

LESSON C SUMMARY (CONTD.)

To determine highest row and column subscripts


in a two-dimensional array:
Use GetUpperBound method
Syntax:
arrayName.GetUpperBound(0)
arrayName.GetUpperBound(1)

58

Вам также может понравиться