⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0087.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0086.html">Prev</a> | <a href="tij0088.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Arrays</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Most
of the necessary introduction to <A NAME="Index718"></A>arrays
is in the last section of Chapter 4, which shows 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?
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">There
are two issues that distinguish arrays from other types of collections: <A NAME="Index719"></A>efficiency
and <A NAME="Index720"></A>type.
The array is the most efficient way that Java provides to store and access a
sequence of objects (actually, object handles). 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 handles from the old one to the new one. This is the behavior of
the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class, which will be studied later in the chapter. However, because of the
overhead of this size flexibility, a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is measurably less efficient than an array.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
<A NAME="Index721"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class in C++ 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>does</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
know the type of objects it holds, but it has a different drawback when
compared with arrays in Java: the C++ 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;s
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>operator[]</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
doesn&#8217;t do bounds checking, so you can run past the end. (It&#8217;s
possible, however, to ask how big the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is, and the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>at(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>does</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
perform bounds checking.) In Java, you get bounds checking regardless of
whether you&#8217;re using an array or a collection &#8211; you&#8217;ll get a <A NAME="Index722"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>RuntimeException</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if you exceed the bounds. As you&#8217;ll learn in Chapter 9, this type of
exception indicates a programmer error and thus you don&#8217;t need to check
for it in your code. As an aside, the reason the C++ 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
doesn&#8217;t check bounds with every access is speed &#8211; in Java you have
the constant performance overhead of bounds checking all the time for both
arrays and collections.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
other generic collection classes that will be studied in this chapter, <A NAME="Index723"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
<A NAME="Index724"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and <A NAME="Index725"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
all deal with objects as if they had no specific type. That is, they treat them
as type <A NAME="Index726"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
the root class of all classes in Java. This works fine from one standpoint: you
need to build only one collection, and any Java object will go into that
collection. (Except for primitives &#8211; these can be placed in collections
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 collections: 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&#8217;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&#8217;s not as if it&#8217;s riskier one way or the other, it&#8217;s just
nicer if the compiler points it out to you, faster at run-time, and
there&#8217;s less likelihood that the end user will get surprised by an
exception.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">For
efficiency and type checking it&#8217;s always worth trying to use an array if
you can. However, when you&#8217;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 collection classes provided by Java.
</FONT><a name="_Toc408018564"></a><a name="_Toc375545348"></a><P></DIV>
<A NAME="Heading242"></A><H3 ALIGN=LEFT>
Arrays
are first-class objects
<P><A NAME="Index727"></A><A NAME="Index728"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Regardless
of what type of array you&#8217;re working with, the array identifier is
actually a handle to a true object that&#8217;s created on the heap. The heap
object can be created either implicitly, as part of the array initialization
syntax, or explicitly with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>new</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
expression. Part of the heap object (in fact, the only field or method you can
access) is the read-only 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>length</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
member that tells you how many elements can be stored in that array object. <A NAME="Index729"></A><A NAME="Index730"></A>The
&#8216;
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>[]</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;
syntax is the only other access that you have to the array object.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following example shows the various ways that an array can be initialized, and
how the array handles can be assigned to different array objects. It also shows
that <A NAME="Index731"></A>arrays
of objects and <A NAME="Index732"></A>arrays
of primitives are almost identical in their use. The only difference is that
arrays of objects hold handles while arrays of primitives hold the primitive
values directly. (See page 
<A HREF=" PAGE#Running_programs">97</A>
if you have trouble executing this program.)
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: ArraySize.java</font>
<font color="#009900">// Initialization &amp; re-assignment of arrays</font>
<font color="#0000ff">package</font> c08;

<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 handle</font>
    Weeble[] b = <font color="#0000ff">new</font> Weeble[5]; <font color="#009900">// Null handles</font>
    Weeble[] c = <font color="#0000ff">new</font> Weeble[4];
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; c.length; i++)
      c[i] = <font color="#0000ff">new</font> Weeble();
    Weeble[] d = {
      <font color="#0000ff">new</font> Weeble(), <font color="#0000ff">new</font> Weeble(), <font color="#0000ff">new</font> Weeble()
    };
    <font color="#009900">// Compile error: variable a not initialized:</font>
    <font color="#009900">//!System.out.println("a.length=" + a.length);</font>
    System.out.println("b.length = " + b.length);
    <font color="#009900">// The handles inside the array are </font>
    <font color="#009900">// automatically initialized to null:</font>
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; b.length; i++)
      System.out.println("b[" + i + "]=" + b[i]);
    System.out.println("c.length = " + c.length);
    System.out.println("d.length = " + d.length);
    a = d;
    System.out.println("a.length = " + a.length);
    <font color="#009900">// Java 1.1 initialization syntax:</font>
    a = <font color="#0000ff">new</font> Weeble[] {
      <font color="#0000ff">new</font> Weeble(), <font color="#0000ff">new</font> Weeble()
    };
    System.out.println("a.length = " + a.length);

    <font color="#009900">// Arrays of primitives:</font>
    <font color="#0000ff">int</font>[] e; <font color="#009900">// Null handle</font>
    <font color="#0000ff">int</font>[] f = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[5];
    <font color="#0000ff">int</font>[] g = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[4];
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; g.length; i++)
      g[i] = i*i;
    <font color="#0000ff">int</font>[] h = { 11, 47, 93 };
    <font color="#009900">// Compile error: variable e not initialized:</font>
    <font color="#009900">//!System.out.println("e.length=" + e.length);</font>
    System.out.println("f.length = " + f.length);
    <font color="#009900">// The primitives inside the array are</font>
    <font color="#009900">// automatically initialized to zero:</font>
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; f.length; i++)
      System.out.println("f[" + i + "]=" + f[i]);
    System.out.println("g.length = " + g.length);
    System.out.println("h.length = " + h.length);
    e = h;
    System.out.println("e.length = " + e.length);
    <font color="#009900">// Java 1.1 initialization syntax:</font>
    e = <font color="#0000ff">new</font> <font color="#0000ff">int</font>[] { 1, 2 };
    System.out.println("e.length = " + e.length);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
the output from the program:
</FONT><P></DIV>

<font color="#990000"><PRE>b.length = 5
b[0]=<font color="#0000ff">null</font>
b[1]=<font color="#0000ff">null</font>
b[2]=<font color="#0000ff">null</font>
b[3]=<font color="#0000ff">null</font>
b[4]=<font color="#0000ff">null</font>
c.length = 4
d.length = 3
a.length = 3
a.length = 2
f.length = 5
f[0]=0
f[1]=0
f[2]=0
f[3]=0
f[4]=0
g.length = 4
h.length = 3
e.length = 3
e.length = 2 </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
array 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>a</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is initially just a <A NAME="Index733"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handle, and the compiler prevents you from doing anything with this handle
until you&#8217;ve properly initialized it. The array 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>b</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is initialized to point to an array of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Weeble</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handles, but no actual 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Weeble</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects are ever placed in that array. However, you can still ask what the size
of the array is, since 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>b</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is pointing to a legitimate object. This brings up a slight drawback: you
can&#8217;t find out how many elements are actually 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>in</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
the array, since 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>length</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
tells you only how many elements 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>can</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
be placed in the array; that is, the size of the array object, not the number
of elements it actually holds. However, when an array object is created its
handles are automatically initialized to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
so you can see whether a particular array slot has an object in it by checking
to see whether it&#8217;s 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Similarly, an array of primitives is automatically initialized to zero for
numeric types, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>null
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">for
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>char</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -