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

📄 tij313.htm

📁 这也是我们java老师给我们的thinking in java的一些资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    <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>);
    System.out.println(<font color=#004488>"a1 = "</font> + Arrays2.toString(a1));
    Arrays.fill(a2, (<font color=#0000ff>byte</font>)11);
    System.out.println(<font color=#004488>"a2 = "</font> + Arrays2.toString(a2));
    Arrays.fill(a3, 'x');
    System.out.println(<font color=#004488>"a3 = "</font> + Arrays2.toString(a3));
    Arrays.fill(a4, (<font color=#0000ff>short</font>)17);
    System.out.println(<font color=#004488>"a4 = "</font> + Arrays2.toString(a4));
    Arrays.fill(a5, 19);
    System.out.println(<font color=#004488>"a5 = "</font> + Arrays2.toString(a5));
    Arrays.fill(a6, 23);
    System.out.println(<font color=#004488>"a6 = "</font> + Arrays2.toString(a6));
    Arrays.fill(a7, 29);
    System.out.println(<font color=#004488>"a7 = "</font> + Arrays2.toString(a7));
    Arrays.fill(a8, 47);
    System.out.println(<font color=#004488>"a8 = "</font> + Arrays2.toString(a8));
    Arrays.fill(a9, <font color=#004488>"Hello"</font>);
    System.out.println(<font color=#004488>"a9 = "</font> + Arrays.asList(a9));
    <font color=#009900>// Manipulating ranges:</font>
    Arrays.fill(a9, 3, 5, <font color=#004488>"World"</font>);
    System.out.println(<font color=#004488>"a9 = "</font> + Arrays.asList(a9));
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"a1 = [true, true, true, true, true, true]"</font>,
      <font color=#004488>"a2 = [11, 11, 11, 11, 11, 11]"</font>,
      <font color=#004488>"a3 = [x, x, x, x, x, x]"</font>,
      <font color=#004488>"a4 = [17, 17, 17, 17, 17, 17]"</font>,
      <font color=#004488>"a5 = [19, 19, 19, 19, 19, 19]"</font>,
      <font color=#004488>"a6 = [23, 23, 23, 23, 23, 23]"</font>,
      <font color=#004488>"a7 = [29.0, 29.0, 29.0, 29.0, 29.0, 29.0]"</font>,
      <font color=#004488>"a8 = [47.0, 47.0, 47.0, 47.0, 47.0, 47.0]"</font>,
      <font color=#004488>"a9 = [Hello, Hello, Hello, Hello, Hello, Hello]"</font>,
      <font color=#004488>"a9 = [Hello, Hello, Hello, World, World, Hello]"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>You can either fill the entire array or, as the last two statements show, 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. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1276" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775739"></a><a name="Heading10709"></a>Copying an array<br></h3>
<p><a name="Index982"></a><a name="Index983"></a>The Java standard library provides a <b>static </b>method, <b>System.arraycopy(&#160;)</b><a name="Index984"></a>, 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&#146;s an example that manipulates arrays of <b>int</b>:<br></p>

<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c11:CopyingArrays.java</font>
<font color=#009900>// Using System.arraycopy()</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<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>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <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>[7];
    <font color=#0000ff>int</font>[] j = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[10];
    Arrays.fill(i, 47);
    Arrays.fill(j, 99);
    System.out.println(<font color=#004488>"i = "</font> + Arrays2.toString(i));
    System.out.println(<font color=#004488>"j = "</font> + Arrays2.toString(j));
    System.arraycopy(i, 0, j, 0, i.length);
    System.out.println(<font color=#004488>"j = "</font> + Arrays2.toString(j));
    <font color=#0000ff>int</font>[] k = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[5];
    Arrays.fill(k, 103);
    System.arraycopy(i, 0, k, 0, k.length);
    System.out.println(<font color=#004488>"k = "</font> + Arrays2.toString(k));
    Arrays.fill(k, 103);
    System.arraycopy(k, 0, i, 0, k.length);
    System.out.println(<font color=#004488>"i = "</font> + Arrays2.toString(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, <font color=#0000ff>new</font> Integer(99));
    System.out.println(<font color=#004488>"u = "</font> + Arrays.asList(u));
    System.out.println(<font color=#004488>"v = "</font> + Arrays.asList(v));
    System.arraycopy(v, 0, u, u.length/2, v.length);
    System.out.println(<font color=#004488>"u = "</font> + Arrays.asList(u));
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"i = [47, 47, 47, 47, 47, 47, 47]"</font>,
      <font color=#004488>"j = [99, 99, 99, 99, 99, 99, 99, 99, 99, 99]"</font>,
      <font color=#004488>"j = [47, 47, 47, 47, 47, 47, 47, 99, 99, 99]"</font>,
      <font color=#004488>"k = [47, 47, 47, 47, 47]"</font>,
      <font color=#004488>"i = [103, 103, 103, 103, 103, 47, 47]"</font>,
      <font color=#004488>"u = [47, 47, 47, 47, 47, 47, 47, 47, 47, 47]"</font>,
      <font color=#004488>"v = [99, 99, 99, 99, 99]"</font>,
      <font color=#004488>"u = [47, 47, 47, 47, 47, 99, 99, 99, 99, 99]"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The arguments to <b>arraycopy(&#160;)</b> are the source array, the offset into the source array from whence to start copying, the destination array, the offset into the destination array where the copying begins, and the number of elements to copy. Naturally, any violation of the array boundaries will cause an exception. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1277" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>The example shows that both primitive arrays and object arrays can be copied. However, if you copy arrays of objects, then only the references get copied&#151;there&#146;s no duplication of the objects themselves. This is called a <i>shallow copy</i> (see Appendix A). <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1278" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc375545350"></a><a name="_Toc24775740"></a><a name="Heading10759"></a>Comparing
arrays<br></h3>
<p><a name="Index985"></a><a name="Index986"></a><b>Arrays</b> provides the overloaded method <b>equals(&#160;)</b> to compare entire arrays for equality. Again, these are overloaded for all the primitives and for <b>Object</b>. To be equal, the arrays must have the same number of elements, and each element must be equivalent to each corresponding element in the other array, using the <b>equals(&#160;) </b>for each element. (For primitives, that primitive&#146;s wrapper class <b>equals(&#160;)</b> is used; for example, <b>Integer.equals(&#160;) </b>for <b>int</b>.)<b> </b>For example:<br></p>

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

<font color=#0000ff>public</font> <font color=#0000ff>class</font> ComparingArrays {
  <font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
  <font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
    <font color=#0000ff>int</font>[] a1 = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[10];
    <font color=#0000ff>int</font>[] a2 = <font color=#0000ff>new</font> <font color=#0000ff>int</font>[10];
    Arrays.fill(a1, 47);
    Arrays.fill(a2, 47);
    System.out.println(Arrays.equals(a1, a2));
    a2[3] = 11;
    System.out.println(Arrays.equals(a1, a2));
    String[] s1 = <font color=#0000ff>new</font> String[5];
    Arrays.fill(s1, <font color=#004488>"Hi"</font>);
    String[] s2 = {<font color=#004488>"Hi"</font>, <font color=#004488>"Hi"</font>, <font color=#004488>"Hi"</font>, <font color=#004488>"Hi"</font>, <font color=#004488>"Hi"</font>};
    System.out.println(Arrays.equals(s1, s2));
    monitor.expect(<font color=#0000ff>new</font> String[] {
      <font color=#004488>"true"</font>,
      <font color=#004488>"false"</font>,
      <font color=#004488>"true"</font>
    });
  }
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Originally, <b>a1</b> and <b>a2</b> are exactly equal, so the output is &#147;true,&#148; but then one of the elements is changed, which makes the result &#147;false.&#148; In the last case, all the elements of <b>s1</b> point to the same object, but <b>s2</b> has five unique objects. However, array equality is based on contents (via <b>Object.equals(&#160;)</b>) , so the result is &#147;true.&#148; <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1279" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775741"></a><a name="Heading10789"></a>Array element
comparisons<br></h3>
<p><a name="Index987"></a>One of the missing features in the Java 1.0 and 1.1 libraries was algorithmic operations&#151;even simple sorting. This was a rather confusing situation to someone expecting an adequate standard library. Fortunately, Java 2 remedied the situation, at least for the sorting problem. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1280" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p><a name="Index988"></a>A problem with writing generic sorting code is that sorting must perform comparisons based on the actual type of the object. Of course, one approach is to write a different sorting method for every different type, but you should be able to recognize that this does not produce code that is easily reused for new types. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap09_1281" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>A primary goal of programming design is to &#147;separate things that change from things that stay the same,&#148; and here, the code that stays the same is the general sort algorithm, but the thing that changes from one use to the next is the way objects are compared. So instead of placing the comparison code into many different sort routines, the technique of the <a name="Index989"></a><i>callback</i> is used. With a callback, the part of the code that varies from case to case is separated, and the part of the code that&#146;s always t

⌨️ 快捷键说明

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