Title: Arrays
18
2Now go, write it before them in a table, and
note it in a book. Isaiah 308
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
3OBJECTIVES
- 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- 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
5Fig. 8.1 A 12-element array.
6Common 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.
7Good 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.
8Outline
InitArray.cs
9Outline
InitArray.cs
10Good 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.
11Outline
InitArray.cs (1 of 2)
12Outline
InitArray.cs (2 of 2)
13Common Programming Error 8.2
- Assigning a value to a named constant after it
has been initialized is a compilation error.
14Common Programming Error 8.3
- Attempting to declare a named constant without
initializing it is a compilation error.
15Outline
SumArray.cs
16Outline
BarChart.cs (1 of 2)
17Outline
BarChart.cs (2 of 2)
18Outline
RollDie.cs
19Outline
StudentPoll.cs (1 of 2)
20Outline
StudentPoll.cs (2 of 2)
21Error-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.
22Error-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.
23Outline
Card.cs
24Outline
DeckOfCards.cs (1 of 2)
25Outline
DeckOfCards.cs (2 of 2)
26Outline
DeckOfCardsTest.cs (1 of 2)
27Outline
DeckOfCardsTest.cs (2 of 2)
28Outline
ForEachTest.cs
29Outline
PassArray.cs (1 of 3)
30Outline
PassArray.cs (2 of 3)
31Outline
PassArray.cs (3 of 3)
32Performance 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
arraysboth of these problems cause poor
performance.
33Outline
ArrayReferenceTest.cs (1 of 5)
34Outline
ArrayReferenceTest.cs (2 of 5)
35Outline
ArrayReferenceTest.cs (3 of 5)
36Outline
ArrayReferenceTest.cs (4 of 5)
37Outline
ArrayReferenceTest.cs (5 of 5)
38Software Engineering Observation 8.1
- When a method receives a reference-type parameter
by value, a copy of the objects reference is
passed. This prevents a method from overwriting
references passed to that method. In the vast
majority of cases, protecting the callers
reference from modification is the desired
behavior. If you encounter a situation where you
truly want the called procedure to modify the
callers reference, pass the reference-type
parameter using keyword refbut, again, such
situations are rare.
39Software 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 callers object.
40Outline
GradeBook.cs (1 of 6)
41Outline
GradeBook.cs (2 of 6)
42Outline
GradeBook.cs (3 of 6)
43Outline
GradeBook.cs (4 of 6)
44Outline
GradeBook.cs (5 of 6)
45Outline
GradeBook.cs (6 of 6)
46Software 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 classs 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.
47Outline
GradeBookTest.cs (1 of 2)
48Outline
GradeBookTest.cs (2 of 2)
49Fig. 8.17 Rectangular array with three rows
and four columns.
50Fig. 8.18 Jagged array with three rows of
different lengths.
51Outline
InitArray.cs (1 of 3)
52Outline
InitArray.cs (2 of 3)
53Outline
InitArray.cs (3 of 3)
54Outline
GradeBook.cs (1 of 7)
55Outline
GradeBook.cs (2 of 7)
56Outline
GradeBook.cs (3 of 7)
57Outline
GradeBook.cs (4 of 7)
58Outline
GradeBook.cs (5 of 7)
59Outline
GradeBook.cs (6 of 7)
60Outline
GradeBook.cs (7 of 7)
61Outline
GradeBookTest.cs (1 of 2)
62Outline
GradeBookTest.cs (2 of 2)
63Outline
VarargsTest.cs (1 of 2)
64Outline
VarargsTest.cs (2 of 2)
65Common 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.
66Outline
InitArray.cs (1 of 3)
67Outline
InitArray.cs (2 of 3)
68Outline
InitArray.cs (3 of 3)
69Fig. 8.24 Collaborations in the ATM system.
70Fig. 8.25 Communication diagram of the ATM
executing a balance inquiry.
71Fig. 8.26 Communication diagram for executing
a BalanceInquiry.
72Fig. 8.27 Sequence diagram that models a
Withdrawal executing.
73Fig. 8.28 Sequence diagram that models a
Deposit executing.