Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2  2006 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 To go beyond is as wrong as to fall short. — Confucius Begin at the beginning, … and go on till you come to the end: then stop. — Lewis Carroll

3  2006 Pearson Education, Inc. All rights reserved. 3 OBJECTIVES In this chapter you will learn:  What arrays are.  To use arrays to store data in and retrieve data from lists and tables of values.  To declare arrays, initialize arrays and refer to individual elements of arrays.  To use the foreach statement to iterate through arrays.  To pass arrays to methods.  To declare and manipulate multidimensional arrays.  To write methods that use variable-length argument lists.  To read command-line arguments into an application.

4  2006 Pearson Education, Inc. All rights reserved. 4 8.1 Introduction 8.2 Arrays 8.3 Declaring and Creating Arrays 8.4 Examples Using Arrays 8.5 Case Study: Card Shuffling and Dealing Simulation 8.6 foreach Statement 8.7 Passing Arrays and Array Elements to Methods 8.8 Passing Arrays by Value and by Reference 8.9 Case Study: Class GradeBook Using an Array to Store Grades 8.10 Multidimensional Arrays 8.11 Case Study: Class GradeBook Using a Rectangular Array 8.12 Variable-Length Argument Lists 8.13 Using Command-Line Arguments 8.14 (Optional) Software Engineering Case Study: Collaboration Among Objects in the ATM System 8.15 Wrap-Up

5  2006 Pearson Education, Inc. All rights reserved. 5 Fig. 8.1 | A 12-element array.

6  2006 Pearson Education, Inc. All rights reserved. 6 Common Programming Error 8.1 In an array declaration, specifying the number of elements in the square brackets of the declaration (e.g., int[ 12 ] c; ) is a syntax error.

7  2006 Pearson Education, Inc. All rights reserved. 7 Good Programming Practice 8.1 For readability, declare only one variable per declaration. Keep each declaration on a separate line and include a comment describing the variable being declared.

8  2006 Pearson Education, Inc. All rights reserved. 8 Outline InitArray.cs

9  2006 Pearson Education, Inc. All rights reserved. 9 Outline InitArray.cs

10  2006 Pearson Education, Inc. All rights reserved. 10 Good Programming Practice 8.2 Constants also are called named constants. Such variables often make applications more readable than applications that use literal values (e.g., 10 )—a named constant such as ARRAY_LENGTH clearly indicates its purpose, whereas a literal value could have different meanings based on the context in which it is used. Another advantage to using named constants is that if the value of the constant must be changed, it is necessary to change it only in the declaration, thus reducing the cost of maintaining the code.

11  2006 Pearson Education, Inc. All rights reserved. 11 Outline InitArray.cs (1 of 2)

12  2006 Pearson Education, Inc. All rights reserved. 12 Outline InitArray.cs (2 of 2)

13  2006 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 8.2 Assigning a value to a named constant after it has been initialized is a compilation error.

14  2006 Pearson Education, Inc. All rights reserved. 14 Common Programming Error 8.3 Attempting to declare a named constant without initializing it is a compilation error.

15  2006 Pearson Education, Inc. All rights reserved. 15 Outline SumArray.cs

16  2006 Pearson Education, Inc. All rights reserved. 16 Outline BarChart.cs (1 of 2)

17  2006 Pearson Education, Inc. All rights reserved. 17 Outline BarChart.cs (2 of 2)

18  2006 Pearson Education, Inc. All rights reserved. 18 Outline RollDie.cs

19  2006 Pearson Education, Inc. All rights reserved. 19 Outline StudentPoll.cs (1 of 2)

20  2006 Pearson Education, Inc. All rights reserved. 20 Outline StudentPoll.cs (2 of 2)

21  2006 Pearson Education, Inc. All rights reserved. 21 Error-Prevention Tip 8.1 An exception indicates that an error has occurred in an application. You often can write code to recover from an exception and continue application execution, rather than abnormally terminating the application. Exception handling is discussed in Chapter 12.

22  2006 Pearson Education, Inc. All rights reserved. 22 Error-Prevention Tip 8.2 When writing code to loop through an array, ensure that the array index remains greater than or equal to 0 and less than the length of the array. The loop-continuation condition should prevent the accessing of elements outside this range.

23  2006 Pearson Education, Inc. All rights reserved. 23 Outline Card.cs

24  2006 Pearson Education, Inc. All rights reserved. 24 Outline DeckOfCards.cs (1 of 2)

25  2006 Pearson Education, Inc. All rights reserved. 25 Outline DeckOfCards.cs (2 of 2)

26  2006 Pearson Education, Inc. All rights reserved. 26 Outline DeckOfCardsTest.cs (1 of 2)

27  2006 Pearson Education, Inc. All rights reserved. 27 Outline DeckOfCardsTest.cs (2 of 2)

28  2006 Pearson Education, Inc. All rights reserved. 28 Outline ForEachTest.cs

29  2006 Pearson Education, Inc. All rights reserved. 29 Outline PassArray.cs (1 of 3)

30  2006 Pearson Education, Inc. All rights reserved. 30 Outline PassArray.cs (2 of 3)

31  2006 Pearson Education, Inc. All rights reserved. 31 Outline PassArray.cs (3 of 3)

32  2006 Pearson Education, Inc. All rights reserved. 32 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 would consume considerable storage for the copies of the arrays— both of these problems cause poor performance.

33  2006 Pearson Education, Inc. All rights reserved. 33 Outline ArrayReferenceTest.cs (1 of 5)

34  2006 Pearson Education, Inc. All rights reserved. 34 Outline ArrayReferenceTest.cs (2 of 5)

35  2006 Pearson Education, Inc. All rights reserved. 35 Outline ArrayReferenceTest.cs (3 of 5)

36  2006 Pearson Education, Inc. All rights reserved. 36 Outline ArrayReferenceTest.cs (4 of 5)

37  2006 Pearson Education, Inc. All rights reserved. 37 Outline ArrayReferenceTest.cs (5 of 5)

38  2006 Pearson Education, Inc. All rights reserved. 38 Software Engineering Observation 8.1 When a method receives a reference-type parameter by value, a copy of the object’s reference is passed. This prevents a method from overwriting references passed to that method. In the vast majority of cases, protecting the caller’s reference from modification is the desired behavior. If you encounter a situation where you truly want the called procedure to modify the caller’s reference, pass the reference-type parameter using keyword ref — but, again, such situations are rare.

39  2006 Pearson Education, Inc. All rights reserved. 39 Software Engineering Observation 8.2 In C#, objects (including arrays) are passed by reference by default. So, a called method receiving a reference to an object in a caller can change the caller’s object.

40  2006 Pearson Education, Inc. All rights reserved. 40 Outline GradeBook.cs (1 of 6)

41  2006 Pearson Education, Inc. All rights reserved. 41 Outline GradeBook.cs (2 of 6)

42  2006 Pearson Education, Inc. All rights reserved. 42 Outline GradeBook.cs (3 of 6)

43  2006 Pearson Education, Inc. All rights reserved. 43 Outline GradeBook.cs (4 of 6)

44  2006 Pearson Education, Inc. All rights reserved. 44 Outline GradeBook.cs (5 of 6)

45  2006 Pearson Education, Inc. All rights reserved. 45 Outline GradeBook.cs (6 of 6)

46  2006 Pearson Education, Inc. All rights reserved. 46 Software Engineering Observation 8.3 A test harness (or 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. Test data can be placed directly into an array with an array initializer, it can come from the user at the keyboard, it can come from a file (as you will see in Chapter 18) or it can come from a network (as you will see in Chapter 23). After passing this data to the class’s constructor to instantiate the object, the test harness should call the object to test its methods and manipulate its data. Gathering data in the test harness like this allows the class to manipulate data from several sources.

47  2006 Pearson Education, Inc. All rights reserved. 47 Outline GradeBookTest.cs (1 of 2)

48  2006 Pearson Education, Inc. All rights reserved. 48 Outline GradeBookTest.cs (2 of 2)

49  2006 Pearson Education, Inc. All rights reserved. 49 Fig. 8.17 | Rectangular array with three rows and four columns.

50  2006 Pearson Education, Inc. All rights reserved. 50 Fig. 8.18 | Jagged array with three rows of different lengths.

51  2006 Pearson Education, Inc. All rights reserved. 51 Outline InitArray.cs (1 of 3)

52  2006 Pearson Education, Inc. All rights reserved. 52 Outline InitArray.cs (2 of 3)

53  2006 Pearson Education, Inc. All rights reserved. 53 Outline InitArray.cs (3 of 3)

54  2006 Pearson Education, Inc. All rights reserved. 54 Outline GradeBook.cs (1 of 7)

55  2006 Pearson Education, Inc. All rights reserved. 55 Outline GradeBook.cs (2 of 7)

56  2006 Pearson Education, Inc. All rights reserved. 56 Outline GradeBook.cs (3 of 7)

57  2006 Pearson Education, Inc. All rights reserved. 57 Outline GradeBook.cs (4 of 7)

58  2006 Pearson Education, Inc. All rights reserved. 58 Outline GradeBook.cs (5 of 7)

59  2006 Pearson Education, Inc. All rights reserved. 59 Outline GradeBook.cs (6 of 7)

60  2006 Pearson Education, Inc. All rights reserved. 60 Outline GradeBook.cs (7 of 7)

61  2006 Pearson Education, Inc. All rights reserved. 61 Outline GradeBookTest.cs (1 of 2)

62  2006 Pearson Education, Inc. All rights reserved. 62 Outline GradeBookTest.cs (2 of 2)

63  2006 Pearson Education, Inc. All rights reserved. 63 Outline VarargsTest.cs (1 of 2)

64  2006 Pearson Education, Inc. All rights reserved. 64 Outline VarargsTest.cs (2 of 2)

65  2006 Pearson Education, Inc. All rights reserved. 65 Common Programming Error 8.4 Using the params modifier with a parameter in the middle of a method parameter list is a syntax error. The params modifier may be used only with the last parameter of the parameter list.

66  2006 Pearson Education, Inc. All rights reserved. 66 Outline InitArray.cs (1 of 3)

67  2006 Pearson Education, Inc. All rights reserved. 67 Outline InitArray.cs (2 of 3)

68  2006 Pearson Education, Inc. All rights reserved. 68 Outline InitArray.cs (3 of 3)

69  2006 Pearson Education, Inc. All rights reserved. 69 Fig. 8.24 | Collaborations in the ATM system.

70  2006 Pearson Education, Inc. All rights reserved. 70 Fig. 8.25 | Communication diagram of the ATM executing a balance inquiry.

71  2006 Pearson Education, Inc. All rights reserved. 71 Fig. 8.26 | Communication diagram for executing a BalanceInquiry.

72  2006 Pearson Education, Inc. All rights reserved. 72 Fig. 8.27 | Sequence diagram that models a Withdrawal executing.

73  2006 Pearson Education, Inc. All rights reserved. 73 Fig. 8.28 | Sequence diagram that models a Deposit executing.


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

Similar presentations


Ads by Google