Java Reference
In-Depth Information
7.1
Introduction
7.2
Arrays
7.3
Declaring and Creating Arrays
7.4
Examples Using Arrays
7.4.1 Creating and Initializing an Array
7.4.2 Using an Array Initializer
7.4.3 Calculating the Values to Store in an
Array
7.4.4 Summing the Elements of an Array
7.4.5 Using Bar Charts to Display Array
Data Graphically
7.4.6 Using the Elements of an Array as
Counters
7.4.7 Using Arrays to Analyze Survey Results
7.5
Exception Handling: Processing the
Incorrect Response
7.5.1 The
try
Statement
7.5.2 Executing the
catch
Block
7.5.3
toString
Method of the Exception
Parameter
7.6
Case Study: Card Shuffling and
Dealing Simulation
7.7
Enhanced
for
Statement
7.8
Passing Arrays to Methods
7.9
Pass-By-Value vs. Pass-By-Reference
7.10
Case Study: Class
GradeBook
Using
an Array to Store Grades
7.11
Multidimensional Arrays
7.12
Case Study: Class
GradeBook
Using
a Two-Dimensional Array
7.13
Variable-Length Argument Lists
7.14
Using Command-Line Arguments
7.15
Class
Arrays
7.16
Introduction to Collections and Class
ArrayList
7.17
(Optional) GUI and Graphics Case
Study: Drawing Arcs
7.18
Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises |
Special Section: Building Your Own Computer | Making a Difference
This chapter introduces
data structures
—collections of related data items.
Array
objects
are data structures consisting of related data items of the same type. Arrays make it conve-
nient to process related groups of values. Arrays remain the same length once they're cre-
ated. We study data structures in depth in Chapters 16-21.
After discussing how arrays are declared, created and initialized, we present practical
examples that demonstrate common array manipulations. We introduce Java's
exception-
handling
mechanism and use it to allow a program to continue executing when it attempts
to access an array element that does not exist. We also present a case study that examines
how arrays can help simulate the shuffling and dealing of playing cards in a card-game
application. We introduce Java's
enhanced
for
statement
, which allows a program to access
the data in an array more easily than does the counter-controlled
for
statement presented
in Section 5.3. We build two versions of an instructor
GradeBook
case study that use arrays
to maintain sets of student grades
in memory
and analyze student grades. We show how to
use
variable-length argument lists
to create methods that can be called with varying num-
bers of arguments, and we demonstrate how to process
command-line arguments
in method
main
. Next, we present some common array manipulations with
static
methods of class
Arrays
from the
java.util
package.
Although commonly used, arrays have limited capabilities. For instance, you must
specify an array's size, and if at execution time you wish to modify it, you must do so by
creating a new array. At the end of this chapter, we introduce one of Java's prebuilt data