Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2009 Pearson Education, Inc. All rights reserved. 1 8 8 Arrays.

Similar presentations


Presentation on theme: " 2009 Pearson Education, Inc. All rights reserved. 1 8 8 Arrays."— Presentation transcript:

1  2009 Pearson Education, Inc. All rights reserved. 1 8 8 Arrays

2  2009 Pearson Education, Inc. All rights reserved. 2 Now go, write it before them in a table, and note it in a book. – Isaiah 30:8 With sobs and tears he sorted out Those of the largest size … – Lewis Carroll Attempt the end, and never stand to doubt; Nothing’s so hard, but search will find it out. – Robert Herrick

3  2009 Pearson Education, Inc. All rights reserved. 3 Begin at the beginning,... and go on till you come to the end: then stop. – Lewis Carroll To go beyond is as wrong as to fall short – Confucius

4  2009 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To use the array data structure; arrays are objects.  How arrays are used to store, sort and search lists and tables of values.  To declare, initialize and refer to individual elements of arrays.  To pass arrays to methods using ByVal and ByRef.

5  2009 Pearson Education, Inc. All rights reserved. 5 OBJECTIVES  To declare and manipulate multidimensional arrays, especially rectangular arrays and jagged arrays.  To create variable-length parameter lists.  To use the For Each … Next statement to iterate through the elements of arrays without using a loop counter.

6  2009 Pearson Education, Inc. All rights reserved. 6 8.1Introduction 8.2Arrays 8.3Declaring and Allocating Arrays 8.4Examples Using Arrays 8.5Case Study: Card Shuffling and Dealing Simulation 8.6Passing an Array to a Method 8.7 For Each … Next Repetition Statement 8.8 GradeBook Case Study: Using an Array to Store Grades 8.9Sorting an Array with Method Sort of Class Array

7  2009 Pearson Education, Inc. All rights reserved. 7 8.10Searching Arrays 8.11Rectangular Arrays 8.12 GradeBook Case Study: Using a Rectangular Array 8.13Variable-Length Parameter Lists 8.14Jagged Arrays 8.15Changing the Size of an Array at Execution Time: Using the ReDim Statement 8.16Passing Arrays: ByVal vs. ByRef 8.17(Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System

8  2009 Pearson Education, Inc. All rights reserved. 8 8.2 Arrays An array is a group of variables (elements) containing values of the same type. The first element is the zeroth element. The elements of array c are c(0), c(1), c(2) and so on. The position number in parentheses is more formally called an index (or a subscript).

9  2009 Pearson Education, Inc. All rights reserved. 9 Fig. 8.1 | Array consisting of 12 elements. 8.2 Arrays (Cont.) Figure 8.1 shows a representation of an integer array called c.

10  2009 Pearson Education, Inc. All rights reserved. 10 Common Programming Error 8.1 Array indices begin at 0, which means that the “seventh element of the array” has the index 6, whereas “array element seven” has the index 7 and is actually the eighth element of the array. We refer to all array elements simply by their indexed names—such as c(0) rather than “the first element of c.” 8.2 Arrays (Cont.)

11  2009 Pearson Education, Inc. All rights reserved. 11 The length of array c is determined by the following expression: c.Length Method GetUpperBound returns the index of the last element in the array (one less than the length): c.GetUpperBound(0) 8.2 Arrays (Cont.)

12  2009 Pearson Education, Inc. All rights reserved. 12 8.3 Declaring and Allocating Arrays To declare an array, provide the name and type: Dim c As Integer() Dim c() As Integer The parentheses indicate that c is an array. Arrays are objects, so they must be allocated using keyword New: c = New Integer(11) {} c = New Integer(0 To 11) {} Array bounds determine what indices can be used. In both examples the array bounds are 0 and 11. Common Programming Error 8.2 Explicitly setting the lower bound of an array to a value other than 0 is a compilation error.

13  2009 Pearson Education, Inc. All rights reserved. 13 8.3 Declaring and Allocating Arrays (cont.) The braces ( { and } ) specify the initial values of the elements in the array. Dim numbers As Integer() numbers = New Integer() {1, 2, 3, 6} The two preceding statements can be combined into a single statement: Dim numbers As Integer() = New Integer() {1, 2, 3, 6} This can be shortened further: Dim numbers As Integer() = {1, 2, 3, 6} Dim numbers(5) As Integer

14  2009 Pearson Education, Inc. All rights reserved. 14 Outline CreateArray.vb ( 1 of 2 ) Outline The program in Fig. 8.2 uses the New operator to allocate an array of 10 Integer elements. array can reference an array of Integer s. Initializing array to an array of Integer s. Length returns the number of elements in the array. The For statement displays the index and value of each element. Fig. 8.2 | Creating an array. (Part 1 of 2.)

15  2009 Pearson Education, Inc. All rights reserved. 15 Outline CreateArray.vb ( 2 of 2 ) Fig. 8.2 | Creating an array. (Part 2 of 2.)

16  2009 Pearson Education, Inc. All rights reserved. 16 Outline InitArray.vb ( 1 of 2 ) Figure 8.3 creates two 10-element integer arrays and sets their element values. array1 is initialized to a new array. array2 is initialized to be the same length as array1. Initializing the elements in array2 to even integers 2 - 20. Fig. 8.3 | Initializing array elements with an array initializer and a For statement. (Part 1 of 2.)

17  2009 Pearson Education, Inc. All rights reserved. 17 Outline InitArray.vb ( 2 of 2 ) Fig. 8.3 | Initializing array elements with an array initializer and a For statement. (Part 2 of 2.)

18  2009 Pearson Education, Inc. All rights reserved. 18 Outline SumArray.vb array is initialized to an array of Integer s. The For... statement sums the elements of array. Fig. 8.4 | Computing the sum of the elements in an array.

19  2009 Pearson Education, Inc. All rights reserved. 19 8.4 Examples Using Arrays (Cont.) 8.4.4 Using Arrays to Analyze Survey Results Consider the following problem statement: Forty students were asked to rate on a scale of 1 to 10 the quality of the food in the student cafeteria. Place the 40 responses in an integer array and determine the frequency of each rating.

20  2009 Pearson Education, Inc. All rights reserved. 20 Outline StudentPoll.vb ( 1 of 2 ) This is a typical array-processing application (Fig. 8.5). The For statement iterates through responses and increments frequency. Fig. 8.5 | Simple student-poll analysis program. (Part 1 of 2.)

21  2009 Pearson Education, Inc. All rights reserved. 21 Outline StudentPoll.vb ( 2 of 2 ) Common Programming Error 8.3 When a program is executed, array element indices are checked for validity. If an attempt is made to use an invalid index to access an element, Visual Basic generates an IndexOutOfRangeException. Error-Prevention Tip 8.1 When looping through an array, the array index should remain between 0 and the upper bound of the array. Fig. 8.5 | Simple student-poll analysis program. (Part 2 of 2.)

22  2009 Pearson Education, Inc. All rights reserved. 22 Outline BarChart.vb ( 1 of 2 ) Figure 8.6 displays data by creating a bar chart with asterisks ( * ). Writing asterisks representing the value of each element. Fig. 8.6 | Bar chart printing program. (Part 1 of 2.)

23  2009 Pearson Education, Inc. All rights reserved. 23 Outline BarChart.vb ( 2 of 2 ) Fig. 8.6 | Bar chart printing program. (Part 2 of 2.)

24  2009 Pearson Education, Inc. All rights reserved. 24 Outline RollDie.vb ( 1 of 4 ) An array version of the dice roll-counting application is shown in Fig. 8.7. Initializing frequency as an Integer array. Fig. 8.7 | Using arrays to eliminate a Select Case statement. (Part 1 of 4.)

25  2009 Pearson Education, Inc. All rights reserved. 25 Outline Recording rolls in frequency. Lines 36–40 replace lines 35– 47 of Fig. 7.17 RollDie.vb ( 2 of 4 ) Fig. 8.7 | Using arrays to eliminate a Select Case statement. (Part 2 of 4.)

26  2009 Pearson Education, Inc. All rights reserved. 26 Outline Lines 62–75 of Fig. 7.17 are replaced by line 54. Producing a random number from 1 to 6 RollDie.vb ( 3 of 4 ) Fig. 8.7 | Using arrays to eliminate a Select Case statement. (Part 3 of 4.)

27  2009 Pearson Education, Inc. All rights reserved. 27 Outline RollDie.vb ( 4 of 4 ) Fig. 8.7 | Using arrays to eliminate a Select Case statement. (Part 4 of 4.)

28  2009 Pearson Education, Inc. All rights reserved. 28 Outline Card.vb An array also can hold reference types. Class Card (Fig. 8.8) contains two String instance variables— face and suit —that refer to a specific Card. ToString is called implicitly when an object is output. Fig. 8.8 | Card class represents a playing card.

29  2009 Pearson Education, Inc. All rights reserved. 29 Outline DeckOfCards.vb ( 1 of 3 ) Class DeckOfCards (Fig. 8.9) declares an instance variable array named deck, which consists of Card objects. deck references an array of card s. Card objects take values from the faces and suits arrays. Fig. 8.9 | DeckOfCards class represents a deck of playing cards that can be shuffled and dealt one at a time. (Part 1 of 3.)

30  2009 Pearson Education, Inc. All rights reserved. 30 Outline The deck array is created. The constructor uses a For statement to fill the deck array. DeckOfCards.vb ( 2 of 3 ) Fig. 8.9 | DeckOfCards class represents a deck of playing cards that can be shuffled and dealt one at a time. (Part 2 of 3.)

31  2009 Pearson Education, Inc. All rights reserved. 31 Outline Determines if any Card s remain to be dealt. Method Shuffle randomly swaps the Card s in the deck. DeckOfCards.vb ( 3 of 3 ) Fig. 8.9 | DeckOfCards class represents a deck of playing cards that can be shuffled and dealt one at a time. (Part 3 of 3.)

32  2009 Pearson Education, Inc. All rights reserved. 32 Outline DeckOfCardsTest.vb ( 1 of 2 ) Figure 8.10 demonstrates the card dealing and shuffling capabilities of class DeckOfCards. Format specifier {0,-18} outputs the first argument after the String in a left-justified field of width 18. Invoking cards ’s Shuffle method. The For statement deals all 52 Card s in the deck and outputs their names. Fig. 8.10 | Card shuffling and dealing (all 52 cards are dealt). (Part 1 of 2.)

33  2009 Pearson Education, Inc. All rights reserved. 33 Outline DeckOfCardsTest.vb ( 2 of 2 ) Fig. 8.10 | Card shuffling and dealing (all 52 cards are dealt). (Part 2 of 2.)

34  2009 Pearson Education, Inc. All rights reserved. 34 8.6 Passing an Array to a Method To pass an array as an argument, use the name of the array without parentheses: Dim hourlyTemperatures As Integer() = New Integer(24) {} DisplayDayData(hourlyTemperatures) The method header for DayData might be written as: Sub DisplayDayData(ByVal temperatureData As Integer())

35  2009 Pearson Education, Inc. All rights reserved. 35 Outline PassArray.vb ( 1 of 5 ) array1 is passed by reference to method ModifyArray Fig. 8.11 | Passing arrays and individual array elements to methods. (Part 1 of 5.)

36  2009 Pearson Education, Inc. All rights reserved. 36 Outline array1(3) is passed by value to method ModifyElementBy Val. array1(3) is passed by reference to method ModifyElementBy Ref. PassArray.vb ( 2 of 5 ) Fig. 8.11 | Passing arrays and individual array elements to methods. (Part 2 of 5.)

37  2009 Pearson Education, Inc. All rights reserved. 37 Outline ModifyArray doubles each element (passed by value). ModifyElementByVal doubles an element passed by value. PassArray.vb ( 3 of 5 ) Fig. 8.11 | Passing arrays and individual array elements to methods. (Part 3 of 5.)

38  2009 Pearson Education, Inc. All rights reserved. 38 Outline ModifyElementByRef doubles an element passed by reference. PassArray.vb ( 4 of 5 ) Fig. 8.11 | Passing arrays and individual array elements to methods. (Part 4 of 5.)

39  2009 Pearson Education, Inc. All rights reserved. 39 Outline PassArray.vb (5 of 5 ) Common Programming Error 8.4 When passing an array to a method, including an empty pair of parentheses after the array name is a syntax error. Fig. 8.11 | Passing arrays and individual array elements to methods. (Part 5 of 5.)

40  2009 Pearson Education, Inc. All rights reserved. 40 Outline ForEach.vb The For Each … Next repetition statement iterates through the values in a data structure, such as an array. The For Each header specifies an element variable and the array. Finding the minimum value in the array. Fig. 8.12 | Using For Each... Next with an array.

41  2009 Pearson Education, Inc. All rights reserved. 41 Outline GradeBook.vb ( 1 of 7 ) Array grades is declared as an instance variable. Fig. 8.13 | GradeBook class using an array to store test grades. (Part 1 of 7.)

42  2009 Pearson Education, Inc. All rights reserved. 42 Outline ProcessGrades outputs a grade report. GradeBook.vb ( 2 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 2 of 7.)

43  2009 Pearson Education, Inc. All rights reserved. 43 Outline GetMinimum loops through the array and compares values to lowGrade. ProcessGrades outputs a grade report. GradeBook.vb ( 3 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 3 of 7.)

44  2009 Pearson Education, Inc. All rights reserved. 44 Outline GradeBook.vb ( 4 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 4 of 7.)

45  2009 Pearson Education, Inc. All rights reserved. 45 Outline Using a For Each statement to total the values in grades. GradeBook.vb ( 5 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 5 of 7.)

46  2009 Pearson Education, Inc. All rights reserved. 46 Outline Counting the frequency of each letter grade level. The format string indicates the format D2 (two decimal digits). GradeBook.vb ( 6 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 6 of 7.)

47  2009 Pearson Education, Inc. All rights reserved. 47 Outline Printing the student number and grade for each student. GradeBook.vb ( 7 of 7 ) Fig. 8.13 | GradeBook class using an array to store test grades. (Part 7 of 7.)

48  2009 Pearson Education, Inc. All rights reserved. 48 Outline GradeBookTest.vb ( 1 of 3 ) Initializing gradesArray to an array of grade values. Fig. 8.14 | GradeBookTest creates a GradeBook object using an array of grades, then invokes method ProcessGrades to analyze them. (Part 1 of 2.)

49  2009 Pearson Education, Inc. All rights reserved. 49 Outline GradeBookTest.vb ( 2 of 3 ) Fig. 8.14 | GradeBookTest creates a GradeBook object using an array of grades, then invokes method ProcessGrades to analyze them. (Part 2 of 2.)

50  2009 Pearson Education, Inc. All rights reserved. 50 Outline GradeBookTest.vb ( 3 of 3 ) Software Engineering Observation 8.1 A test application is responsible for creating an object of the class being tested and providing it with data. This data could come from any of several sources. After passing this data to the class’s constructor to instantiate the object, the test harness should call the object’s methods to verify that they work properly.

51  2009 Pearson Education, Inc. All rights reserved. 51 Outline SortTest.vb ( 1 of 3 ) Sorting data is one of the most popular computing applications. Method Sort of class Array sorts an array’s elements into ascending order (Fig. 8.15). Fig. 8.15 | Sorting an array with method Array.Sort. (Part 1 of 3.)

52  2009 Pearson Education, Inc. All rights reserved. 52 Outline 10 random values are assigned to integerArray and the contents are displayed. array is sorted in ascending order. SortTest.vb ( 2 of 3 ) Fig. 8.15 | Sorting an array with method Array.Sort. (Part 2 of 3.)

53  2009 Pearson Education, Inc. All rights reserved. 53 Outline SortTest.vb ( 3 of 3 ) a)b) Fig. 8.15 | Sorting an array with method Array.Sort. (Part 3 of 3.)

54  2009 Pearson Education, Inc. All rights reserved. 54 Outline LinearSearch.vb Search compares each element of an array with a search key. Fig. 8.16 | Method for performing a linear search.

55  2009 Pearson Education, Inc. All rights reserved. 55 Outline LinearSearchTest.vb ( 1 of 3 ) Fig. 8.17 | Linear search of an array. (Part 1 of 3.)

56  2009 Pearson Education, Inc. All rights reserved. 56 Outline Storing element as the result of a linear search. LinearSearchTest.vb ( 2 of 3 ) Fig. 8.17 | Linear search of an array. (Part 2 of 3.)

57  2009 Pearson Education, Inc. All rights reserved. 57 Outline LinearSearchTest.vb ( 3 of 3 ) Fig. 8.17 | Linear search of an array. (Part 3 of 3.)

58  2009 Pearson Education, Inc. All rights reserved. 58 Outline BinarySearchTest.vb ( 1 of 4 ) If an array is sorted, the high-speed binary search technique can be used. Figure 8.18 uses method BinarySearch of class Array Sort must be invoked before passing the array. Fig. 8.18 | Binary search of an array. (Part 1 of 4.)

59  2009 Pearson Education, Inc. All rights reserved. 59 Outline BinarySearchTest.vb ( 2 of 4 ) Fig. 8.18 | Binary search of an array. (Part 2 of 4.)

60  2009 Pearson Education, Inc. All rights reserved. 60 Outline The search key is converted to an integer. Storing element as the result of a linear search. Displaying the search result. BinarySearchTest.vb ( 3 of 4 ) Fig. 8.18 | Binary search of an array. (Part 3 of 4.)

61  2009 Pearson Education, Inc. All rights reserved. 61 Outline BinarySearchTest.vb ( 4 of 4 ) Fig. 8.18 | Binary search of an array. (Part 4 of 4.)

62  2009 Pearson Education, Inc. All rights reserved. 62 8.11 Rectangular Arrays Fig. 8.19 | Two-dimensional array with three rows and four columns. Rectangular arrays represent tables of values in rows and columns. Figure 8.19 illustrates a 3-by-4 array.

63  2009 Pearson Education, Inc. All rights reserved. 63 8.11 Rectangular Arrays (Cont.) A two-dimensional rectangular array can be declared and initialized like this: Dim numbers As Integer(,) = New Integer(1,1) {} numbers(0, 0) = 1 ' leftmost element in row 0 numbers(0, 1) = 2 ' rightmost element in row 0 numbers(1, 0) = 3 ' leftmost element in row 1 numbers(1, 1) = 4 ' rightmost element in row 1

64  2009 Pearson Education, Inc. All rights reserved. 64 8.11 Rectangular Arrays (Cont.) The initialization can also be written on one line: Dim numbers As Integer(,) = New Integer(,) {{1, 2}, {3, 4}} The preceding declaration can also be written as Dim numbers As Integer(,) = {{1, 2}, {3, 4}}

65  2009 Pearson Education, Inc. All rights reserved. 65 Outline RectangularArray.vb ( 1 of 2 ) The program in Fig. 8.20 demonstrates the initialization of a rectangular array and the use of nested For Next loop. Initializing array1 to a rectangular array Fig. 8.20 | Initializing a rectangular array. (Part 1 of 2.)

66  2009 Pearson Education, Inc. All rights reserved. 66 Outline The inner For … Next statement traverses the columns within a row. RectangularArray.vb ( 2 of 2 ) The outer For … Next statement traverses the rows Fig. 8.20 | Initializing a rectangular array. (Part 2 of 2.)

67  2009 Pearson Education, Inc. All rights reserved. 67 Outline GradeBook.vb (1 of 8 ) Figure 8.21 contains a version of class GradeBook that uses a rectangular array. Method OutputBarChart uses nested loops to produce a bar chart of student grades. Initializing grades to a rectangular array Setting grades to a rectangular array Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 1 of 8.)

68  2009 Pearson Education, Inc. All rights reserved. 68 Outline GradeBook.vb ( 2 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 2 of 8.)

69  2009 Pearson Education, Inc. All rights reserved. 69 Outline GetMinimum determines the lowest grade of any student. GradeBook.vb ( 3 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 3 of 8.)

70  2009 Pearson Education, Inc. All rights reserved. 70 Outline GetMinimum determines the lowest grade of any student. GetMaximum determines the highest grade of any student. GradeBook.vb ( 4 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 4 of 8.)

71  2009 Pearson Education, Inc. All rights reserved. 71 Outline GetAverage determines a particular student’s average. GradeBook.vb ( 5 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 5 of 8.)

72  2009 Pearson Education, Inc. All rights reserved. 72 Outline Nested loops output a bar chart of student grades. GradeBook.vb ( 6 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 6 of 8.)

73  2009 Pearson Education, Inc. All rights reserved. 73 Outline OutputGrades outputs the array in a tabular format. GradeBook.vb ( 7 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 7 of 8.)

74  2009 Pearson Education, Inc. All rights reserved. 74 Outline OutputGrades outputs the array in a tabular format. GradeBook.vb ( 8 of 8 ) Fig. 8.21 | GradeBook class using a rectangular array to store grades. (Part 8 of 8.)

75  2009 Pearson Education, Inc. All rights reserved. 75 Outline GradeBookTest.vb ( 1 of 2 ) gradesArray is initialized to a rectangular array of Integer s. A GradeBook object is constructed from a name String and the grades array. Fig. 8.22 | Creates GradeBook object using a rectangular array of grades, then invokes method processGrades to analyze them. (Part 1 of 2.)

76  2009 Pearson Education, Inc. All rights reserved. 76 Outline GradeBookTest.vb ( 2 of 2 ) Fig. 8.22 | Creates GradeBook object using a rectangular array of grades, then invokes method processGrades to analyze them. (Part 2 of 2.)

77  2009 Pearson Education, Inc. All rights reserved. 77 Outline ParamArrayTest.vb ( 1 of 3 ) It’s possible to create methods that receive a variable number of arguments, using keyword ParamArray. Figure 8.23 calls method AnyNumberOfArguments three times, passing a different number of values each time. A different number of arguments is used in each call. Fig. 8.23 | Creating variable-length parameter lists. (Part 1 of 2.)

78  2009 Pearson Education, Inc. All rights reserved. 78 Outline Printing any arguments passed to the method. ParamArrayTest.vb ( 2 of 3 ) Determining whether the number of arguments is zero. Fig. 8.23 | Creating variable-length parameter lists. (Part 2 of 2.)

79  2009 Pearson Education, Inc. All rights reserved. 79 Outline ParamArrayTest.vb ( 3 of 3 ) Common Programming Error 8.5 Attempting to declare a parameter variable to the right of the ParamArray array variable is a syntax error. Common Programming Error 8.6 Using ByRef with ParamArray is a syntax error.

80  2009 Pearson Education, Inc. All rights reserved. 80 Outline JaggedArray.vb ( 1 of 2 ) Jagged arrays are maintained as arrays of arrays. The program in Fig. 8.24 demonstrates the use of a jagged array. The declaration of array1 creates a jagged array of three arrays. Fig. 8.24 | Initializing a jagged array. (Part 1 of 2.)

81  2009 Pearson Education, Inc. All rights reserved. 81 Outline The nested For statements traverse the jagged array. JaggedArray.vb ( 2 of 2 ) Fig. 8.24 | Initializing a jagged array. (Part 2 of 2.)

82  2009 Pearson Education, Inc. All rights reserved. 82 Outline ReDimTest.vb ( 1 of 3 ) The ReDim statement enables you to change the array size (Fig. 8.25). To save the original data stored in an array, follow ReDim with Preserve. The ReDim statement changes the upper bound of the array. Fig. 8.25 | Using ReDim statements to change the array size. (Part 1 of 3.)

83  2009 Pearson Education, Inc. All rights reserved. 83 Outline Preserve indicates the existing array elements are kept when the array is resized. ReDimTest.vb ( 2 of 3 ) Fig. 8.25 | Using ReDim statements to change the array size. (Part 2 of 3.)

84  2009 Pearson Education, Inc. All rights reserved. 84 Outline ReDimTest.vb ( 3 of 3 ) Fig. 8.25 | Using ReDim statements to change the array size. (Part 3 of 3.)

85  2009 Pearson Education, Inc. All rights reserved. 85 8.16 Passing Arrays: ByVal vs. ByRef Reference types passed via keyword ByVal are actually passed by reference, meaning that changes are made to the original objects. When a reference-type object is passed with ByRef, the method gains control over the reference in the caller. Performance Tip 8.1 Passing arrays and other objects by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would waste time and consume considerable storage for the copies of the arrays—both of these problems cause poor performance.

86  2009 Pearson Education, Inc. All rights reserved. 86 Outline ArrayReferenceTest.vb ( 1 of 6 ) The program in Fig. 8.26 demonstrates the difference between passing a reference ByVal and ByRef. firstArrayCopy now refers to the same object as firstArray. The For statement prints the contents of firstArray. Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 1 of 6.)

87  2009 Pearson Education, Inc. All rights reserved. 87 Outline ArrayReferenceTest.vb ( 2 of 6 ) Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 2 of 6.)

88  2009 Pearson Education, Inc. All rights reserved. 88 Outline ArrayReferenceTest.vb ( 3 of 6 ) Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 3 of 6.)

89  2009 Pearson Education, Inc. All rights reserved. 89 Outline The For statement prints the contents of firstArray. ArrayReferenceTest.vb ( 4 of 6 ) Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 4 of 6.)

90  2009 Pearson Education, Inc. All rights reserved. 90 Outline FirstDouble multiplies the values of all the elements in the array by 2. SecondDouble receives its array argument ByRef. ArrayReferenceTest.vb ( 5 of 6 ) Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 5 of 6.)

91  2009 Pearson Education, Inc. All rights reserved. 91 Outline ArrayReferenceTest.vb ( 6 of 6 ) Software Engineering Observation 8.2 Using ByVal to receive a reference-type object parameter does not cause the object to pass by value. ByVal causes only the object’s reference to pass by value. This prevents a called method from overwriting a reference in the caller. In the vast majority of cases, protecting the caller’s reference from modification is the desired behavior. Fig. 8.26 | Passing an array reference with ByVal and ByRef. (Part 6 of 6.)

92  2009 Pearson Education, Inc. All rights reserved. 92 8.17 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System A collaboration consists of an object sending a message to an object of another class via method calls. Figure 8.27 lists the collaborations that can be derived from the requirements document.

93  2009 Pearson Education, Inc. All rights reserved. 93 8.17 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System (Cont.) Fig. 8.27 | Collaborations in the ATM system. (Part 1 of 2.)

94  2009 Pearson Education, Inc. All rights reserved. 94 8.17 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System (Cont.) Fig. 8.27 | Collaborations in the ATM system. (Part 2 of 2.)

95  2009 Pearson Education, Inc. All rights reserved. 95 8.17 (Optional) Software Engineering Case Study:Collaboration Among Objects in the ATM System (Cont.) UML interaction diagrams model the behavior of a system by modeling object interactions. The communication diagram emphasizes which objects participate in collaborations. The sequence diagram emphasizes when messages are sent between objects.

96  2009 Pearson Education, Inc. All rights reserved. 96 Fig. 8.28 | Communication diagram of the ATM executing a BalanceInquiry. Figure 8.28 shows a communication diagram that models the ATM executing a BalanceInquiry. The filled arrow indicates that since this is a synchronous call, the caller stops until the receiver returns control. 8.17 (Optional) Software Engineering Case Study:Collaboration Among Objects in the ATM System (Cont.)

97  2009 Pearson Education, Inc. All rights reserved. 97 Figure 8.29 shows a communication diagram that models the interactions in a BalanceInquiry. The number to the left of a message name indicates the order in which the message is passed. A message passed within another message is called a nested message, indicated by decimal numbers. 8.17 (Optional) Software Engineering Case Study:Collaboration Among Objects in the ATM System (Cont.)

98  2009 Pearson Education, Inc. All rights reserved. 98 Fig. 8.29 |.Communication diagram for executing a BalanceInquiry. 8.17 (Optional) Software Engineering Case Study:Collaboration Among Objects in the ATM System (Cont.)

99  2009 Pearson Education, Inc. All rights reserved. 99 Figure 8.30 shows a sequence diagram modeling the sequence of interactions that occur when a Withdrawal executes. The dotted line is that object’s lifeline, which represents the progression of time. An activation, shown as a thin vertical rectangle, indicates that an object is executing. 8.17 (Optional) Software Engineering Case Study:Collaboration Among Objects in the ATM System (Cont.)

100  2009 Pearson Education, Inc. All rights reserved. 100 8.17 Collaboration Among Objects in the ATM System (Cont.) Fig. 8.30 | Sequence diagram that models a Withdrawal executing


Download ppt " 2009 Pearson Education, Inc. All rights reserved. 1 8 8 Arrays."

Similar presentations


Ads by Google