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

📄 chap09.htm

📁 Thinking in Java, 2nd edition
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    <font color=#0000ff>private</font> <font color=#0000ff>int</font> mod = 10000;
    <font color=#0000ff>public</font> RandIntGenerator() {}
    <font color=#0000ff>public</font> RandIntGenerator(<font color=#0000ff>int</font> modulo) {
      mod = modulo;
    }
    <font color=#0000ff>public</font> <font color=#0000ff>int</font> next() { 
      <font color=#0000ff>return</font> r.nextInt() % mod; 
    }
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>class</font> RandLongGenerator 
  <font color=#0000ff>implements</font> LongGenerator {
    <font color=#0000ff>public</font> <font color=#0000ff>long</font> next() { <font color=#0000ff>return</font> r.nextLong(); }
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>class</font> RandFloatGenerator 
  <font color=#0000ff>implements</font> FloatGenerator {
    <font color=#0000ff>public</font> <font color=#0000ff>float</font> next() { <font color=#0000ff>return</font> r.nextFloat(); }
  }
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>class</font> RandDoubleGenerator 
  <font color=#0000ff>implements</font> DoubleGenerator {
    <font color=#0000ff>public</font> <font color=#0000ff>double</font> next() {<font color=#0000ff>return</font> r.nextDouble();}
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To fill an array of elements using a
generator, the <B>fill(&#160;)</B> method takes a reference to an appropriate
generator <B>interface</B>, which has a <B>next(&#160;)</B> method that will
somehow produce an object of the right type (depending on how the interface is
implemented). The <B>fill(&#160;)</B> method simply calls <B>next(&#160;)</B>
until the desired range has been filled. Now you can create any generator by
implementing the appropriate <B>interface</B>, and use your generator with
<B>fill(&#160;)</B>. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I24' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER9_I25>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Random data generators are useful for
testing, so a set of inner classes is created to implement all the primitive
generator interfaces, as well as a <B>String</B> generator to represent
<B>Object</B>. You can see that <B>RandStringGenerator</B> uses
<B>RandCharGenerator</B> to fill an array of characters, which is then turned
into a <B>String</B>. The size of the array is determined by the constructor
argument. 
</backtalk:display>
[&nbsp;<a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER9_I25' 
  target="_blank">Add&nbsp;Comment</a>&nbsp;]

<backtalk:display ID=TIJ3_CHAPTER9_I26>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To generate numbers that aren&#8217;t too
large, <B>RandIntGenerator</B> defaults to a modulus of 10,000, but the
overloaded constructor allows you to choose a smaller value.

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

<backtalk:display ID=TIJ3_CHAPTER9_I27>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Here&#8217;s a program to test the
library, and to demonstrate how it is used:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:TestArrays2.java</font>
<font color=#009900>// Test and demonstrate Arrays2 utilities</font>
<font color=#0000ff>import</font> com.bruceeckel.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> TestArrays2 {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>int</font> size = 6;
    <font color=#009900>// Or get the size from the command line:</font>
    <font color=#0000ff>if</font>(args.length != 0)
      size = Integer.parseInt(args[0]);
    <font color=#0000ff>boolean</font>[] a1 = <font color=#0000ff>new</font> <font color=#0000ff>boolean</font>[size];
    <font color=#0000ff>byte</font>[] a2 = <font color=#0000ff>new</font> <font color=#0000ff>byte</font>[size];
    <font color=#0000ff>char</font>[] a3 = <font color=#0000ff>new</font> <font color=#0000ff>char</font>[size];
    <font color=#0000ff>short</font>[] a4 = <font color=#0000ff>new</font> <font color=#0000ff>short</font>[size];
    <font color=#0000ff>int</font>[] a5 = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[size];
    <font color=#0000ff>long</font>[] a6 = <font color=#0000ff>new</font> <font color=#0000ff>long</font>[size];
    <font color=#0000ff>float</font>[] a7 = <font color=#0000ff>new</font> <font color=#0000ff>float</font>[size];
    <font color=#0000ff>double</font>[] a8 = <font color=#0000ff>new</font> <font color=#0000ff>double</font>[size];
    String[] a9 = <font color=#0000ff>new</font> String[size];
    Arrays2.fill(a1, 
      <font color=#0000ff>new</font> Arrays2.RandBooleanGenerator());
    Arrays2.print(a1);
    Arrays2.print(<font color=#004488>"a1 = "</font>, a1);
    Arrays2.print(a1, size/3, size/3 + size/3);
    Arrays2.fill(a2,
      <font color=#0000ff>new</font> Arrays2.RandByteGenerator());
    Arrays2.print(a2);
    Arrays2.print(<font color=#004488>"a2 = "</font>, a2);
    Arrays2.print(a2, size/3, size/3 + size/3);
    Arrays2.fill(a3,
      <font color=#0000ff>new</font> Arrays2.RandCharGenerator());
    Arrays2.print(a3);
    Arrays2.print(<font color=#004488>"a3 = "</font>, a3);
    Arrays2.print(a3, size/3, size/3 + size/3);
    Arrays2.fill(a4,
      <font color=#0000ff>new</font> Arrays2.RandShortGenerator());
    Arrays2.print(a4);
    Arrays2.print(<font color=#004488>"a4 = "</font>, a4);
    Arrays2.print(a4, size/3, size/3 + size/3);
    Arrays2.fill(a5,
      <font color=#0000ff>new</font> Arrays2.RandIntGenerator());
    Arrays2.print(a5);
    Arrays2.print(<font color=#004488>"a5 = "</font>, a5);
    Arrays2.print(a5, size/3, size/3 + size/3);
    Arrays2.fill(a6,
      <font color=#0000ff>new</font> Arrays2.RandLongGenerator());
    Arrays2.print(a6);
    Arrays2.print(<font color=#004488>"a6 = "</font>, a6);
    Arrays2.print(a6, size/3, size/3 + size/3);
    Arrays2.fill(a7,
      <font color=#0000ff>new</font> Arrays2.RandFloatGenerator());
    Arrays2.print(a7);
    Arrays2.print(<font color=#004488>"a7 = "</font>, a7);
    Arrays2.print(a7, size/3, size/3 + size/3);
    Arrays2.fill(a8,
      <font color=#0000ff>new</font> Arrays2.RandDoubleGenerator());
    Arrays2.print(a8);
    Arrays2.print(<font color=#004488>"a8 = "</font>, a8);
    Arrays2.print(a8, size/3, size/3 + size/3);
    Arrays2.fill(a9,
      <font color=#0000ff>new</font> Arrays2.RandStringGenerator(7));
    Arrays2.print(a9);
    Arrays2.print(<font color=#004488>"a9 = "</font>, a9);
    Arrays2.print(a9, size/3, size/3 + size/3);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>size</B> parameter has a default
value, but you can also set it from the command line.

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

<backtalk:display ID=TIJ3_CHAPTER9_I28>
</FONT><A NAME="_Toc481064669"></A><BR></P></DIV>
<A NAME="Heading282"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Filling an array</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java standard library <B>Arrays</B>
also has a <B>fill(&#160;)</B> method, but that is rather trivial&#8212;it only
duplicates a single value into each location, or in the case of objects, copies
the same reference into each location. Using <B>Arrays2.print(&#160;)</B>, the
<A NAME="Index875"></A><B>Arrays.fill(&#160;)</B> methods can be easily
demonstrated:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:FillingArrays.java</font>
<font color=#009900>// Using Arrays.fill()</font>
<font color=#0000ff>import</font> com.bruceeckel.util.*;
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> FillingArrays {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>int</font> size = 6;
    <font color=#009900>// Or get the size from the command line:</font>
    <font color=#0000ff>if</font>(args.length != 0)
      size = Integer.parseInt(args[0]);
    <font color=#0000ff>boolean</font>[] a1 = <font color=#0000ff>new</font> <font color=#0000ff>boolean</font>[size];
    <font color=#0000ff>byte</font>[] a2 = <font color=#0000ff>new</font> <font color=#0000ff>byte</font>[size];
    <font color=#0000ff>char</font>[] a3 = <font color=#0000ff>new</font> <font color=#0000ff>char</font>[size];
    <font color=#0000ff>short</font>[] a4 = <font color=#0000ff>new</font> <font color=#0000ff>short</font>[size];
    <font color=#0000ff>int</font>[] a5 = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[size];
    <font color=#0000ff>long</font>[] a6 = <font color=#0000ff>new</font> <font color=#0000ff>long</font>[size];
    <font color=#0000ff>float</font>[] a7 = <font color=#0000ff>new</font> <font color=#0000ff>float</font>[size];
    <font color=#0000ff>double</font>[] a8 = <font color=#0000ff>new</font> <font color=#0000ff>double</font>[size];
    String[] a9 = <font color=#0000ff>new</font> String[size];
    Arrays.fill(a1, <font color=#0000ff>true</font>);
    Arrays2.print(<font color=#004488>"a1 = "</font>, a1);
    Arrays.fill(a2, (<font color=#0000ff>byte</font>)11);
    Arrays2.print(<font color=#004488>"a2 = "</font>, a2);
    Arrays.fill(a3, 'x');
    Arrays2.print(<font color=#004488>"a3 = "</font>, a3);
    Arrays.fill(a4, (<font color=#0000ff>short</font>)17);
    Arrays2.print(<font color=#004488>"a4 = "</font>, a4);
    Arrays.fill(a5, 19);
    Arrays2.print(<font color=#004488>"a5 = "</font>, a5);
    Arrays.fill(a6, 23);
    Arrays2.print(<font color=#004488>"a6 = "</font>, a6);
    Arrays.fill(a7, 29);
    Arrays2.print(<font color=#004488>"a7 = "</font>, a7);
    Arrays.fill(a8, 47);
    Arrays2.print(<font color=#004488>"a8 = "</font>, a8);
    Arrays.fill(a9, <font color=#004488>"Hello"</font>);
    Arrays2.print(<font color=#004488>"a9 = "</font>, a9);
    <font color=#009900>// Manipulating ranges:</font>
    Arrays.fill(a9, 3, 5, <font color=#004488>"World"</font>);
    Arrays2.print(<font color=#004488>"a9 = "</font>, a9);
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can either fill the entire array,
or&#8212;as the last two statements show&#8212;a range of elements. But since
you can only provide a single value to use for filling using
<B>Arrays.fill(&#160;)</B>, the <B>Arrays2.fill(&#160;)</B> methods produce much
more interesting results.

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

<backtalk:display ID=TIJ3_CHAPTER9_I29>
</FONT><A NAME="_Toc481064670"></A><BR></P></DIV>
<A NAME="Heading283"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Copying an array<BR><A NAME="Index876"></A><A NAME="Index877"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The Java standard library provides a
<B>static </B>method, <A NAME="Index878"></A><B>System.arraycopy(&#160;)</B>,
which can make much faster copies of an array than if you use a <B>for</B> loop
to perform the copy by hand. <B>System.arraycopy(&#160;) </B>is overloaded to
handle all types. Here&#8217;s an example that manipulates arrays of
<B>int</B>:</FONT><BR></P></DIV>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c09:CopyingArrays.java</font>
<font color=#009900>// Using System.arraycopy()</font>
<font color=#0000ff>import</font> com.bruceeckel.util.*;
<font color=#0000ff>import</font> java.util.*;

<font color=#0000ff>public</font> <font color=#0000ff>class</font> CopyingArrays {
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>int</font>[] i = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[25];
    <font color=#0000ff>int</font>[] j = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[25];
    Arrays.fill(i, 47);
    Arrays.fill(j, 99);
    Arrays2.print(<font color=#004488>"i = "</font>, i);
    Arrays2.print(<font color=#004488>"j = "</font>, j);
    System.arraycopy(i, 0, j, 0, i.length);
    Arrays2.print(<font color=#004488>"j = "</font>, j);
    <font color=#0000ff>int</font>[] k = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[10];
    Arrays.fill(k, 103);
    System.arraycopy(i, 0, k, 0, k.length);
    Arrays2.print(<font color=#004488>"k = "</font>, k);
    Arrays.fill(k, 103);
    System.arraycopy(k, 0, i, 0, k.length);
    Arrays2.print(<font color=#004488>"i = "</font>, i);
    <font color=#009900>// Objects:</font>
    Integer[] u = <font color=#0000ff>new</font> Integer[10];
    Integer[] v = <font color=#0000ff>new</font> Integer[5];
    Arrays.fill(u, <font color=#0000ff>new</font> Integer(47));
    Arrays.fill(v, 

⌨️ 快捷键说明

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