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

📄 chap09.htm

📁 java书籍《thinking in java》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The method <B>flavorSet(&#160;)</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&#8212;it&#8217;s a reference. It&#8217;s not
important that the array was created within <B>flavorSet(&#160;)</B>, or that
the array was created anyplace else, for that matter. The garbage collector
takes care of cleaning up the array when you&#8217;re done with it, and the
array will persist for as long as you need it.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I18' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<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(&#160;)</B> chooses flavors randomly, it ensures that a random
choice hasn&#8217;t been picked before. This is performed in a <B>do</B> loop
that keeps making random choices until it finds one that&#8217;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&#8217;s successful,
it adds the entry and finds the next one (<B>i </B>gets incremented). 

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I19' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER9_I20>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>main(&#160;)</B> prints out 20 full
sets of flavors, so you can see that <B>flavorSet(&#160;)</B> chooses the
flavors in a random order each time. It&#8217;s easiest to see this if you
redirect the output into a file. And while you&#8217;re looking at the file,
remember, you just <I>want</I> the ice cream, you don&#8217;t <I>need</I> it.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I20' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<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&#8217;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(&#160;)</B>, to compare two arrays for equality;
<B>fill(&#160;)</B>, to fill an array with a value; <B>sort(&#160;)</B>, to sort
the array; and <B>binarySearch(&#160;)</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&#8217;s a single <B>asList(&#160;)</B> method
that takes any array and turns it into a <B>List</B> container&#8212;which
you&#8217;ll learn about later in this chapter.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I21' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<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&#8217;ll see, the <B>fill(&#160;)</B> method
only takes a single value and places it in the array, so if you wanted&#8212;for
example&#8212;to fill an array with randomly generated numbers,
<B>fill(&#160;)</B> is no help.

</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I22' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<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>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I23' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<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&#8217;s a
lot of nearly duplicated
code</FONT><A NAME="fnB46" HREF="#fn46">[46]</A><FONT FACE="Georgia">. For
example, a &#8220;generator&#8221; interface is required for each type because
the return type of <B>next(&#160;)</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(&#160;)</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(&#160;)</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 &lt; to; i++) {
      System.out.print(a[i]);
      <font color=#0000ff>if</font>(i &lt; 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 &lt; to; i++) {
      System.out.print(a[i]);
      <font color=#0000ff>if</font>(i &lt; 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 &lt; to; i++) {
      System.out.print(a[i]);
      <font color=#0000ff>if</font>(i &lt; 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 + -