📄 chap09.htm
字号:
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The method <B>flavorSet( )</B>
creates an array of <B>String</B> called <B>results</B>. The size of this array
is <B>n</B>, determined by the argument you pass into the method. Then it
proceeds to choose flavors randomly from the array <B>flav</B> and place them
into <B>results</B>, which it finally returns. Returning an array is just like
returning any other object—it’s a reference. It’s not
important that the array was created within <B>flavorSet( )</B>, or that
the array was created anyplace else, for that matter. The garbage collector
takes care of cleaning up the array when you’re done with it, and the
array will persist for as long as you need it.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I18'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I19>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">As an aside, notice that when
<B>flavorSet( )</B> chooses flavors randomly, it ensures that a random
choice hasn’t been picked before. This is performed in a <B>do</B> loop
that keeps making random choices until it finds one that’s not already in
the <B>picked</B> array. (Of course, a <B>String</B> comparison could also have
been performed to see if the random choice was already in the <B>results</B>
array, but <B>String</B> comparisons are inefficient.) If it’s successful,
it adds the entry and finds the next one (<B>i </B>gets incremented).
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I19'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I20>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>main( )</B> prints out 20 full
sets of flavors, so you can see that <B>flavorSet( )</B> chooses the
flavors in a random order each time. It’s easiest to see this if you
redirect the output into a file. And while you’re looking at the file,
remember, you just <I>want</I> the ice cream, you don’t <I>need</I> it.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I20'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I21>
</FONT><A NAME="_Toc481064668"></A><BR></P></DIV>
<A NAME="Heading281"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
The Arrays class</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">In <B>java.util</B>, you’ll find
the <A NAME="Index872"></A><B>Arrays</B> class, which holds a set of
<B>static</B> methods that perform utility functions for arrays. There are four
basic functions: <B>equals( )</B>, to compare two arrays for equality;
<B>fill( )</B>, to fill an array with a value; <B>sort( )</B>, to sort
the array; and <B>binarySearch( )</B>, to find an element in a sorted
array. All of these methods are overloaded for all the primitive types and
<B>Object</B>s. In addition, there’s a single <B>asList( )</B> method
that takes any array and turns it into a <B>List</B> container—which
you’ll learn about later in this chapter.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I21'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I22>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">While useful, the <B>Arrays</B> class
stops short of being fully functional. For example, it would be nice to be able
to easily print the elements of an array without having to code a <B>for</B>
loop by hand every time. And as you’ll see, the <B>fill( )</B> method
only takes a single value and places it in the array, so if you wanted—for
example—to fill an array with randomly generated numbers,
<B>fill( )</B> is no help.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I22'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I23>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Thus it makes sense to supplement the
<B>Arrays</B> class with some additional utilities, which will be placed in the
<B>package</B> <B>com.bruceeckel.util</B> for convenience. These will print an
array of any type, and fill an array with values or objects that are created by
an object called a <I>generator</I> that you can define.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I23'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER9_I24>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index873"></A><FONT FACE="Georgia">Because code needs
to be created for each primitive type as well as <B>Object</B>, there’s a
lot of nearly duplicated
code</FONT><A NAME="fnB46" HREF="#fn46">[46]</A><FONT FACE="Georgia">. For
example, a “generator” interface is required for each type because
the return type of <B>next( )</B> must be different in each
case:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:util:Generator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> Generator {
Object next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:BooleanGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> BooleanGenerator {
<font color=#0000ff>boolean</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:ByteGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> ByteGenerator {
<font color=#0000ff>byte</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:CharGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> CharGenerator {
<font color=#0000ff>char</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:ShortGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> ShortGenerator {
<font color=#0000ff>short</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:IntGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> IntGenerator {
<font color=#0000ff>int</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:LongGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> LongGenerator {
<font color=#0000ff>long</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:FloatGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> FloatGenerator {
<font color=#0000ff>float</font> next();
} <font color=#009900>///:~</font>
<font color=#009900>//: com:bruceeckel:util:DoubleGenerator.java</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> DoubleGenerator {
<font color=#0000ff>double</font> next();
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><A NAME="Index874"></A><FONT FACE="Georgia"><B>Arrays2</B>
contains a variety of <B>print( )</B> functions, overloaded for each type.
You can simply print an array, you can add a message before the array is
printed, or you can print a range of elements within an array. The
<B>print( )</B> code is self-explanatory:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: com:bruceeckel:util:Arrays2.java</font>
<font color=#009900>// A supplement to java.util.Arrays, to provide</font>
<font color=#009900>// additional useful functionality when working</font>
<font color=#009900>// with arrays. Allows any array to be printed,</font>
<font color=#009900>// and to be filled via a user-defined </font>
<font color=#009900>// "generator" object.</font>
<font color=#0000ff>package</font> com.bruceeckel.util;
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> Arrays2 {
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
start(<font color=#0000ff>int</font> from, <font color=#0000ff>int</font> to, <font color=#0000ff>int</font> length) {
<font color=#0000ff>if</font>(from != 0 || to != length)
System.out.print(<font color=#004488>"["</font>+ from +<font color=#004488>":"</font>+ to +<font color=#004488>"] "</font>);
System.out.print(<font color=#004488>"("</font>);
}
<font color=#0000ff>private</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> end() {
System.out.println(<font color=#004488>")"</font>);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(Object[] a) {
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(String msg, Object[] a) {
System.out.print(msg + <font color=#004488>" "</font>);
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(Object[] a, <font color=#0000ff>int</font> from, <font color=#0000ff>int</font> to){
start(from, to, a.length);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = from; i < to; i++) {
System.out.print(a[i]);
<font color=#0000ff>if</font>(i < to -1)
System.out.print(<font color=#004488>", "</font>);
}
end();
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(<font color=#0000ff>boolean</font>[] a) {
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(String msg, <font color=#0000ff>boolean</font>[] a) {
System.out.print(msg + <font color=#004488>" "</font>);
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(<font color=#0000ff>boolean</font>[] a, <font color=#0000ff>int</font> from, <font color=#0000ff>int</font> to) {
start(from, to, a.length);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = from; i < to; i++) {
System.out.print(a[i]);
<font color=#0000ff>if</font>(i < to -1)
System.out.print(<font color=#004488>", "</font>);
}
end();
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(<font color=#0000ff>byte</font>[] a) {
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(String msg, <font color=#0000ff>byte</font>[] a) {
System.out.print(msg + <font color=#004488>" "</font>);
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(<font color=#0000ff>byte</font>[] a, <font color=#0000ff>int</font> from, <font color=#0000ff>int</font> to) {
start(from, to, a.length);
<font color=#0000ff>for</font>(<font color=#0000ff>int</font> i = from; i < to; i++) {
System.out.print(a[i]);
<font color=#0000ff>if</font>(i < to -1)
System.out.print(<font color=#004488>", "</font>);
}
end();
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> print(<font color=#0000ff>char</font>[] a) {
print(a, 0, a.length);
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font>
print(String msg, <font color=#0000ff>char</font>[] a) {
System.out.print(msg + <font color=#004488>" "</font>);
print(a, 0, a.length);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -