📄 chap09.htm
字号:
<font color=#0000ff>new</font> Weeble(), <font color=#0000ff>new</font> Weeble(), <font color=#0000ff>new</font> Weeble()
};
<font color=#009900>// Dynamic aggregate initialization:</font>
a = <font color=#0000ff>new</font> Weeble[] {
<font color=#0000ff>new</font> Weeble(), <font color=#0000ff>new</font> Weeble()
};
System.out.println(<font color=#004488>"a.length="</font> + a.length);
System.out.println(<font color=#004488>"b.length = "</font> + b.length);
<font color=#009900>// The references 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 < b.length; i++)
System.out.println(<font color=#004488>"b["</font> + i + <font color=#004488>"]="</font> + b[i]);
System.out.println(<font color=#004488>"c.length = "</font> + c.length);
System.out.println(<font color=#004488>"d.length = "</font> + d.length);
a = d;
System.out.println(<font color=#004488>"a.length = "</font> + a.length);
<font color=#009900>// Arrays of primitives:</font>
<font color=#0000ff>int</font>[] e; <font color=#009900>// Null reference</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 < 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(<font color=#004488>"f.length = "</font> + 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 < f.length; i++)
System.out.println(<font color=#004488>"f["</font> + i + <font color=#004488>"]="</font> + f[i]);
System.out.println(<font color=#004488>"g.length = "</font> + g.length);
System.out.println(<font color=#004488>"h.length = "</font> + h.length);
e = h;
System.out.println(<font color=#004488>"e.length = "</font> + e.length);
e = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[] { 1, 2 };
System.out.println(<font color=#004488>"e.length = "</font> + e.length);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here’s the output from the
program:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><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></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The array <B>a</B> is initially just a
<A NAME="Index865"></A><B>null </B>reference, and the compiler prevents you from
doing anything with this reference until you’ve properly initialized it.
The array <B>b</B> is initialized to point to an array of <B>Weeble</B>
references, but no actual <B>Weeble</B> objects are ever placed in that array.
However, you can still ask what the size of the array is, since <B>b</B> is
pointing to a legitimate object. This brings up a slight drawback: you
can’t find out how many elements are actually <I>in</I> the array, since
<B>length</B> tells you only how many elements <I>can</I> 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 references are
automatically initialized to <B>null</B>, so you can see whether a particular
array slot has an object in it by checking to see whether it’s
<B>null</B>. Similarly, an array of primitives is automatically initialized to
zero for numeric types, <B>(char)0 </B>for <B>char</B>, and<B> false</B> for
<B>boolean</B>.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I9'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I10>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Array <B>c</B> shows the creation of the
array object followed by the assignment of <B>Weeble</B> objects to all the
slots in the array. Array <B>d</B> shows the “aggregate
initialization” syntax that causes the array object to be created
(implicitly with <B>new</B> on the heap, just like for array <B>c</B>)
<I>and</I> initialized with <B>Weeble</B> objects, all in one statement.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I10'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I11>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index866"></A><A NAME="Index867"></A><FONT FACE="Georgia">The
next array initialization could be thought of as a “dynamic aggregate
initialization.” The aggregate initialization used by <B>d</B> must be
used at the point of <B>d</B>’s definition, but with the second syntax you
can create and initialize an array object anywhere. For example, suppose
<B>hide( )</B> is a method that takes an array of <B>Weeble</B> objects.
You could call it by saying:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>hide(d);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">but you can also dynamically create the
array you want to pass as the argument:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>hide(<font color=#0000ff>new</font> Weeble[] { <font color=#0000ff>new</font> Weeble(), <font color=#0000ff>new</font> Weeble() });</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In some situations this new syntax
provides a more convenient way to write code.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I11'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I12>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The expression:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>a = d;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">shows how you can take a reference
that’s attached to one array object and assign it to another array object,
just as you can do with any other type of object reference. Now both <B>a</B>
and <B>d</B> are pointing to the same array object on the heap.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I12'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I13>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The second part of <B>ArraySize.java</B>
shows that primitive arrays work just like object arrays <I>except</I> that
primitive arrays hold the primitive values directly.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I13'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I14>
</FONT><A NAME="_Toc375545349"></A><BR></P></DIV>
<A NAME="Heading279"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Containers of primitives<BR><A NAME="Index868"></A><A NAME="Index869"></A></H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Container classes can hold only
references to objects. An array, however, can be created to hold primitives
directly, as well as references to objects. It <I>is</I> possible to use the
“wrapper” classes such as <B>Integer</B>, <B>Double,</B> etc. to
place primitive values inside a container, but the wrapper classes for
primitives can be awkward to use. In addition, it’s much more efficient to
create and access an array of primitives than a container of wrapped primitives.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I14'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I15>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Of course, if you’re using a
primitive type and you need the flexibility of a container that automatically
expands when more space is needed, the array won’t work and you’re
forced to use a container of wrapped primitives. You might think that there
should be a specialized type of <B>ArrayList</B> for each of the primitive data
types, but Java doesn’t provide this for you. Some sort of templatizing
mechanism might someday provide a better way for Java to handle this
problem.</FONT><A NAME="fnB45" HREF="#fn45">[45]</A><FONT FACE="Georgia">
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I15'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I16>
</FONT><A NAME="_Toc481064667"></A><BR></P></DIV>
<A NAME="Heading280"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Returning an array</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Suppose you’re writing a method and
you don’t just want to return just one thing, but a whole bunch of things.
Languages like C and C++ make this difficult because you can’t just return
an array, only a pointer to an array. This introduces problems because it
becomes messy to control the lifetime of the array, which easily leads to memory
leaks.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I16'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I17>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index870"></A><A NAME="Index871"></A><FONT FACE="Georgia">Java
takes a similar approach, but you just “return an array.” Actually,
of course, you’re returning a reference to an array, but with Java you
never worry about responsibility for that array—it will be around as long
as you need it, and the garbage collector will clean it up when you’re
done.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I17'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I18>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As an example, consider returning an
array of <B>String</B>:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:IceCream.java</font>
<font color=#009900>// Returning arrays from methods.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> IceCream {
<font color=#0000ff>static</font> String[] flav = {
<font color=#004488>"Chocolate"</font>, <font color=#004488>"Strawberry"</font>,
<font color=#004488>"Vanilla Fudge Swirl"</font>, <font color=#004488>"Mint Chip"</font>,
<font color=#004488>"Mocha Almond Fudge"</font>, <font color=#004488>"Rum Raisin"</font>,
<font color=#004488>"Praline Cream"</font>, <font color=#004488>"Mud Pie"</font>
};
<font color=#0000ff>static</font> String[] flavorSet(<font color=#0000ff>int</font> n) {
<font color=#009900>// Force it to be positive & within bounds:</font>
n = Math.abs(n) % (flav.length + 1);
String[] results = <font color=#0000ff>new</font> String[n];
<font color=#0000ff>boolean</font>[] picked =
<font color=#0000ff>new</font> <font color=#0000ff>boolean</font>[flav.length];
<font color=#0000ff>for</font> (<font color=#0000ff>int</font> i = 0; i < n; i++) {
<font color=#0000ff>int</font> t;
<font color=#0000ff>do</font>
t = (<font color=#0000ff>int</font>)(Math.random() * flav.length);
<font color=#0000ff>while</font> (picked[t]);
results[i] = flav[t];
picked[t] = <font color=#0000ff>true</font>;
}
<font color=#0000ff>return</font> results;
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = 0; i < 20; i++) {
System.out.println(
<font color=#004488>"flavorSet("</font> + i + <font color=#004488>") = "</font>);
String[] fl = flavorSet(flav.length);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> j = 0; j < fl.length; j++)
System.out.println(<font color=#004488>"\t"</font> + fl[j]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -