Java Reference
In-Depth Information
This chapter began our introduction to data structures, exploring the use of arrays to store
data in and retrieve data from lists and tables of values. The chapter examples demonstrated
how to declare an array, initialize an array and refer to individual elements of an array. The
chapter introduced the enhanced
for
statement to iterate through arrays. We used excep-
tion handling to test for
ArrayIndexOutOfBoundsException
s that occur when a program
attempts to access an array element outside the bounds of an array. We also illustrated how
to pass arrays to methods and how to declare and manipulate multidimensional arrays. Fi-
nally, the chapter showed how to write methods that use variable-length argument lists and
how to read arguments passed to a program from the command line.
We introduced the
ArrayList<T>
generic collection, which provides all the function-
ality and performance of arrays, along with other useful capabilities such as dynamic
resizing. We used the
add
methods to add new items to the end of an
ArrayList
and to
insert items in an
ArrayList
. The
remove
method was used to remove the first occurrence
of a specified item, and an overloaded version of
remove
was used to remove an item at a
specified index. We used the
size
method to obtain number of items in the
ArrayList
.
We continue our coverage of data structures in Chapter 16, Generic Collections.
Chapter 16 introduces the Java Collections Framework, which uses generics to allow you
to specify the exact types of objects that a particular data structure will store. Chapter 16
also introduces Java's other predefined data structures. Chapter 16 covers additional
methods of class
Arrays
. You'll be able to use some of the
Arrays
methods discussed in
Chapter 16 after reading the current chapter, but some of the
Arrays
methods require
knowledge of concepts presented later in the topic. Chapter 20 discusses generics, which
enable you to create general models of methods and classes that can be declared once, but
used with many different data types. Chapter 21, Custom Generic Data Structures, shows
how to build dynamic data structures, such as lists, queues, stacks and trees, that can grow
and shrink as programs execute.
We've now introduced the basic concepts of classes, objects, control statements,
methods, arrays and collections. In Chapter 8, we take a deeper look at classes and objects.
Summary
Section 7.1 Introduction
• Arrays (p. 244) are fixed-length data structures consisting of related data items of the same type.
Section 7.2 Arrays
• An array is a group of variables (called elements or components; p. 245) containing values that
all have the same type. Arrays are objects, so they're considered reference types.
• A program refers to any one of an array's elements with an array-access expression (p. 245) that
includes the name of the array followed by the index of the particular element in square brackets
(
[]
; p. 245).
• The first element in every array has index zero (p. 245) and is sometimes called the zeroth element.
• An index must be a nonnegative integer. A program can use an expression as an index.
• An array object knows its own length and stores this information in a
length
instance variable
(p. 246).