📄 chap09.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:TIJ2.rtf
Application Directory:C:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:05/21/2001
Translation Time:10:39:12
Translation Platform:Win32
Number of Output files:23
This File:Chap09.htm
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>9: Holding Your Objects</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README.txt">Viewing Hints</a> ]
[ <a href="RevHist.htm">Revision History</a> ]
[ <a href="http://www.mindview.net/Books/TIJ/">Book Home Page</a> ]
[ <a href="http://www.mindview.net/Etc/MailingList.html">Free Newsletter</a> ] <br>
[ <a href="http://www.mindview.net/Seminars">Seminars</a> ]
[ <a href="http://www.mindview.net/CDs">Seminars on CD ROM</a> ]
[ <a href="http://www.mindview.net/Services">Consulting</a> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 2nd edition, Revision 12</FONT></H2>
<H3><FONT FACE="Verdana">©2000 by Bruce Eckel</FONT></H3>
<FONT FACE="Verdana" size = "-1">
[ <a href="Chap08.htm">Previous Chapter</a> ]
[ <a href="SimpCont.htm">Short TOC</a> ]
[ <a href="Contents.htm">Table of Contents</a> ]
[ <a href="DocIdx.htm">Index</a> ]
[ <a href="Chap10.htm">Next Chapter</a> ]
</FONT>
</CENTER>
</P></DIV><A NAME="_Toc477690729"></A><A NAME="_Toc481064664"></A><A NAME="Heading276"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
9: Holding <BR>Your Objects<A NAME="OLE_LINK2"></A></H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia" SIZE=4><backtalk:display ID=TIJ3_CHAPTER9_I0>
It’s
a fairly simple program that has only a fixed quantity of objects with known
lifetimes.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In general, your programs will always be
creating new objects based on some criteria that will be known only at the time
the program is running. You won’t know until run-time the quantity or even
the exact type of the objects you need. To solve the general programming
problem, you need to be able to create any number of objects, anytime, anywhere.
So you can’t rely on creating a named reference to hold each one of your
objects:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>MyObject myReference;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">since you’ll never know how many of
these you’ll actually need.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I0'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I1>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To solve this rather essential problem,
Java has several ways to hold objects (or rather, references to objects). The
built-in type is the array, which has been discussed before. Also, the Java
utilities library has a reasonably complete set of
<A NAME="Index848"></A><I>container classes</I> (also known as
<A NAME="Index849"></A><I>collection</I> <I>classes</I>, but because the Java 2
libraries use the name <B>Collection</B> to refer to a particular subset of the
library, I shall use the more inclusive term “container”).
Containers provide sophisticated ways to hold and even manipulate your objects.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I1'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I2>
</FONT><A NAME="_Toc375545347"></A><A NAME="_Toc481064665"></A><BR></P></DIV>
<A NAME="Heading277"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Arrays</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Most of the necessary introduction to
<A NAME="Index850"></A>arrays is in the last section of Chapter 4, which showed
how you define and initialize an array. Holding objects is the focus of this
chapter, and an array is just one way to hold objects. But there are a number of
other ways to hold objects, so what makes an array special?
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I2'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I3>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">There are two issues that distinguish
arrays from other types of containers: <A NAME="Index851"></A>efficiency and
<A NAME="Index852"></A>type. The array is the most efficient way that Java
provides to store and randomly access a sequence of objects (actually, object
references). The array is a simple linear sequence, which makes element access
fast, but you pay for this speed: when you create an array object, its size is
fixed and cannot be changed for the lifetime of that array object. You might
suggest creating an array of a particular size and then, if you run out of
space, creating a new one and moving all the references from the old one to the
new one. This is the behavior of the <B>ArrayList </B>class, which will be
studied later in this chapter. However, because of the overhead of this size
flexibility, an <B>ArrayList</B> is measurably less efficient than an array.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I3'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I4>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <A NAME="Index853"></A><B>vector</B>
container class in C++ <I>does</I> know the type of objects it holds, but it has
a different drawback when compared with arrays in Java: the C++
<B>vector</B>’s <B>operator[]</B> doesn’t do bounds checking, so you
can run past the
end</FONT><A NAME="fnB44" HREF="#fn44">[44]</A><FONT FACE="Georgia">. In Java,
you get bounds checking regardless of whether you’re using an array or a
container—you’ll get a
<A NAME="Index854"></A><B>RuntimeException</B> if you exceed the bounds. As
you’ll learn in Chapter 10, this type of exception indicates a programmer
error, and thus you don’t need to check for it in your code. As an aside,
the reason the C++ <B>vector</B> doesn’t check bounds with every access is
speed—in Java you have the constant performance overhead of bounds
checking all the time for both arrays and containers.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I4'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I5>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The other generic container classes that
will be studied in this chapter, <A NAME="Index855"></A><B>List</B>,
<A NAME="Index856"></A><B>Set</B>, and <A NAME="Index857"></A><B>Map</B>, all
deal with objects as if they had no specific type. That is, they treat them as
type <A NAME="Index858"></A><B>Object</B>, the root class of all classes in
Java. This works fine from one standpoint: you need to build only one container,
and any Java object will go into that container. (Except for
primitives—these can be placed in containers as constants using the Java
primitive wrapper classes, or as changeable values by wrapping in your own
class.) This is the second place where an array is superior to the generic
containers: when you create an array, you create it to hold a specific type.
This means that you get compile-time type checking to prevent you from putting
the wrong type in, or mistaking the type that you’re extracting. Of
course, Java will prevent you from sending an inappropriate message to an
object, either at compile-time or at run-time. So it’s not much riskier
one way or the other, it’s just nicer if the compiler points it out to
you, faster at run-time, and there’s less likelihood that the end user
will get surprised by an exception.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I5'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I6>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">For efficiency and type checking
it’s always worth trying to use an array if you can. However, when
you’re trying to solve a more general problem arrays can be too
restrictive. After looking at arrays, the rest of this chapter will be devoted
to the container classes provided by Java.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I6'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I7>
</FONT><A NAME="_Toc375545348"></A><A NAME="_Toc481064666"></A><BR></P></DIV>
<A NAME="Heading278"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Arrays are first-class
objects<BR><A NAME="Index859"></A><A NAME="Index860"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Regardless of what type of array
you’re working with, the array identifier is actually a reference to a
true object that’s created on the heap. This is the object that holds the
references to the other objects, and it can be created either implicitly, as
part of the array initialization syntax, or explicitly with a <B>new</B>
expression. Part of the array object (in fact, the only field or method you can
access) is the read-only <B>length</B> member that tells you how many elements
can be stored in that array object.
<A NAME="Index861"></A><A NAME="Index862"></A>The ‘<B>[]</B>’ syntax
is the only other access that you have to the array object.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I7'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I8>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The following example shows the various
ways that an array can be initialized, and how the array references can be
assigned to different array objects. It also shows that
<A NAME="Index863"></A>arrays of objects and <A NAME="Index864"></A>arrays of
primitives are almost identical in their use. The only difference is that arrays
of objects hold references, while arrays of primitives hold the primitive values
directly.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I8'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I9>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:ArraySize.java</font>
<font color=#009900>// Initialization & re-assignment of arrays.</font>
<font color=#0000ff>class</font> Weeble {} <font color=#009900>// A small mythical creature</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ArraySize {
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#009900>// Arrays of objects:</font>
Weeble[] a; <font color=#009900>// Null reference</font>
Weeble[] b = <font color=#0000ff>new</font> Weeble[5]; <font color=#009900>// Null references</font>
Weeble[] c = <font color=#0000ff>new</font> Weeble[4];
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < c.length; i++)
c[i] = <font color=#0000ff>new</font> Weeble();
<font color=#009900>// Aggregate initialization:</font>
Weeble[] d = {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -