Java Reference
In-Depth Information
(a)
firstNode
lastNode
12
7
11
5
(b)
firstNode
current
lastNode
12
7
11
5
removeItem
Fig. 21.9
|
Graphical representation of operation
removeFromBack
.
and removal operations to the front and the back of the list. In Exercise 21.26, you'll
enhance the
List
class to enable insertions and deletions
anywhere
in the
List
.]
Method
print
(lines 127-146 of Fig. 21.3) first determines whether the list is empty (lines
129-133). If so,
print
displays a message indicating that the list is empty and returns control
to the calling method. Otherwise,
print
outputs the list's data. Line 136 creates
ListNode
current
and initializes it with
firstNode
. While
current
is not
null
, there are more items
in the list. Therefore, line 141 outputs a string representation of
current.data
. Line 142
moves to the next node in the list by assigning the value of reference
current.nextNode
to
current
. This printing algorithm is identical for linked lists, stacks and queues.
As you know, the Java API types (classes, interfaces and
enum
s) are organized in
packages
that
group related types. Packages facilitate software reuse by enabling programs to
import
exist-
ing classes, rather than
copying
them into the folders of each program that uses them. Pro-
grammers use packages to organize program components, especially in large programs. For
example, you might have one package containing the types that make up your program's
graphical user interface, another for the types that manage your application's data and an-
other for the types that communicate with servers over a network. In addition, packages help
you specify unique names for every type you declare, which (as we'll discuss) helps prevent
class-name conflicts. This section introduces how to create and use your own packages.
Much of what we discuss here is handled for you by IDEs such as NetBeans, Eclipse and In-
telliJ IDEA. We focus on creating and using packages with the JDK's command-line tools.