Arrays - PowerPoint PPT Presentation

1 / 73
About This Presentation
Title:

Arrays

Description:

To use arrays to store data in and retrieve data from lists and tables of values. ... For readability, declare only one variable per declaration. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 74
Provided by: pt196
Category:
Tags: arrays | middle

less

Transcript and Presenter's Notes

Title: Arrays


1
8
  • Arrays

2
Now 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
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
  • 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
Fig. 8.1 A 12-element array.
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
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
Outline
InitArray.cs
9
Outline
InitArray.cs
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
Outline
InitArray.cs (1 of 2)
12
Outline
InitArray.cs (2 of 2)
13
Common Programming Error 8.2
  • Assigning a value to a named constant after it
    has been initialized is a compilation error.

14
Common Programming Error 8.3
  • Attempting to declare a named constant without
    initializing it is a compilation error.

15
Outline
SumArray.cs
16
Outline
BarChart.cs (1 of 2)
17
Outline
BarChart.cs (2 of 2)
18
Outline
RollDie.cs
19
Outline
StudentPoll.cs (1 of 2)
20
Outline
StudentPoll.cs (2 of 2)
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
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
Outline
Card.cs
24
Outline
DeckOfCards.cs (1 of 2)
25
Outline
DeckOfCards.cs (2 of 2)
26
Outline
DeckOfCardsTest.cs (1 of 2)
27
Outline
DeckOfCardsTest.cs (2 of 2)
28
Outline
ForEachTest.cs
29
Outline
PassArray.cs (1 of 3)
30
Outline
PassArray.cs (2 of 3)
31
Outline
PassArray.cs (3 of 3)
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
    arraysboth of these problems cause poor
    performance.

33
Outline
ArrayReferenceTest.cs (1 of 5)
34
Outline
ArrayReferenceTest.cs (2 of 5)
35
Outline
ArrayReferenceTest.cs (3 of 5)
36
Outline
ArrayReferenceTest.cs (4 of 5)
37
Outline
ArrayReferenceTest.cs (5 of 5)
38
Software 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.

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 callers object.

40
Outline
GradeBook.cs (1 of 6)
41
Outline
GradeBook.cs (2 of 6)
42
Outline
GradeBook.cs (3 of 6)
43
Outline
GradeBook.cs (4 of 6)
44
Outline
GradeBook.cs (5 of 6)
45
Outline
GradeBook.cs (6 of 6)
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 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.

47
Outline
GradeBookTest.cs (1 of 2)
48
Outline
GradeBookTest.cs (2 of 2)
49
Fig. 8.17 Rectangular array with three rows
and four columns.
50
Fig. 8.18 Jagged array with three rows of
different lengths.
51
Outline
InitArray.cs (1 of 3)
52
Outline
InitArray.cs (2 of 3)
53
Outline
InitArray.cs (3 of 3)
54
Outline
GradeBook.cs (1 of 7)
55
Outline
GradeBook.cs (2 of 7)
56
Outline
GradeBook.cs (3 of 7)
57
Outline
GradeBook.cs (4 of 7)
58
Outline
GradeBook.cs (5 of 7)
59
Outline
GradeBook.cs (6 of 7)
60
Outline
GradeBook.cs (7 of 7)
61
Outline
GradeBookTest.cs (1 of 2)
62
Outline
GradeBookTest.cs (2 of 2)
63
Outline
VarargsTest.cs (1 of 2)
64
Outline
VarargsTest.cs (2 of 2)
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
Outline
InitArray.cs (1 of 3)
67
Outline
InitArray.cs (2 of 3)
68
Outline
InitArray.cs (3 of 3)
69
Fig. 8.24 Collaborations in the ATM system.
70
Fig. 8.25 Communication diagram of the ATM
executing a balance inquiry.
71
Fig. 8.26 Communication diagram for executing
a BalanceInquiry.
72
Fig. 8.27 Sequence diagram that models a
Withdrawal executing.
73
Fig. 8.28 Sequence diagram that models a
Deposit executing.
Write a Comment
User Comments (0)
About PowerShow.com