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

📄 tij0090.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<font color="#990000"><PRE><font color="#009900">//: Statistics.java</font>
<font color="#009900">// Simple demonstration of Hashtable</font>
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">class</font> Counter { 
  <font color="#0000ff">int</font> i = 1; 
  <font color="#0000ff">public</font> String toString() { 
    <font color="#0000ff">return</font> Integer.toString(i); 
  }
}

<font color="#0000ff">class</font> Statistics {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Hashtable ht = <font color="#0000ff">new</font> Hashtable();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; 10000; i++) {
      <font color="#009900">// Produce a number between 0 and 20:</font>
      Integer r = 
        <font color="#0000ff">new</font> Integer((<font color="#0000ff">int</font>)(Math.random() * 20));
      <font color="#0000ff">if</font>(ht.containsKey(r))
        ((Counter)ht.get(r)).i++;
      <font color="#0000ff">else</font>
        ht.put(r, <font color="#0000ff">new</font> Counter());
    }
    System.out.println(ht);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
each time a random number is generated it is wrapped inside an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object so that handle can be used with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
(You can&#8217;t use a primitive with a collection, only an object handle.) The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>containsKey(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method checks to see if this key is already in the collection. (That is, has
the number been found already?) If so, the <A NAME="Index801"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>get(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
methods gets the associated value for the key, which in this case is a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. The value 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
inside the counter is then incremented to indicate that one more of this
particular random number has been found.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">If
the key has not been found yet, the method <A NAME="Index802"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>put(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
will place a new key-value pair into the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Since 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
automatically initializes its variable 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>i</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
to one when it&#8217;s created, it indicates the first occurrence of this
particular random number.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">To
display the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
it is simply printed out. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method moves through all the key-value pairs and calls the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for each one. The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer
toString(&#160;)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is pre-defined, and you can see the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
for 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The output from one run (with some line breaks added) is:
</FONT><P></DIV>

<font color="#990000"><PRE>{19=526, 18=533, 17=460, 16=513, 15=521, 14=495,
 13=512, 12=483, 11=488, 10=487, 9=514, 8=523,
 7=497, 6=487, 5=480, 4=489, 3=509, 2=503, 1=475,
 0=505} </PRE></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
might wonder at the necessity of the class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Counter,</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
which seems like it doesn&#8217;t even have the functionality of the wrapper
class 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Why not use 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">?
Well, you can&#8217;t use an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
because all of the collections can hold only
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
Object
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
handles. After seeing collections the wrapper classes might begin to make a
little more sense to you, since you can&#8217;t put any of the primitive types
in collections. However, the only thing you 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>can</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
do with the Java <A NAME="Index803"></A>wrappers
is to initialize them to a particular value and read that value. That is,
there&#8217;s no way to change a value once a wrapper object has been created.
This makes the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
wrapper immediately useless to solve our problem, so we&#8217;re forced to
create a new class that does satisfy the need.
</FONT><P></DIV>
<A NAME="Heading257"></A><H4 ALIGN=LEFT>
Creating
&#8220;key&#8221; classes
</H4>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
the previous example, a standard library class (
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Integer</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">)
was used as a key for the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
It worked fine as a key, because it has all the necessary wiring to make it
work correctly as a key. But a common pitfall occurs when using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
when you create your own classes to be used as keys. For example, consider a
weather predicting system that matches 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects to 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
objects. It seems fairly straightforward: you create the two classes and use 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as the key and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as the value:
</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: SpringDetector.java</font>
<font color="#009900">// Looks plausible, but doesn't work right.</font>
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">class</font> Groundhog {
  <font color="#0000ff">int</font> ghNumber;
  Groundhog(<font color="#0000ff">int</font> n) { ghNumber = n; }
}

<font color="#0000ff">class</font> Prediction {
  <font color="#0000ff">boolean</font> shadow = Math.random() &gt; 0.5;
  <font color="#0000ff">public</font> String toString() {
    <font color="#0000ff">if</font>(shadow)
      <font color="#0000ff">return</font> "Six more weeks of Winter!";
    <font color="#0000ff">else</font>
      <font color="#0000ff">return</font> "Early Spring!";
  }
}

<font color="#0000ff">public</font> <font color="#0000ff">class</font> SpringDetector {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Hashtable ht = <font color="#0000ff">new</font> Hashtable();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; 10; i++)
      ht.put(<font color="#0000ff">new</font> Groundhog(i), <font color="#0000ff">new</font> Prediction());
    System.out.println("ht = " + ht + "\n");
    System.out.println(
      "Looking up prediction <font color="#0000ff">for</font> groundhog #3:");
    Groundhog gh = <font color="#0000ff">new</font> Groundhog(3);
    <font color="#0000ff">if</font>(ht.containsKey(gh))
      System.out.println((Prediction)ht.get(gh));
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Each
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is given an identity number, so you can look up a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
by saying &#8220;Give me the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">associated
with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
number 3.&#8221; The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
class contains a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that is initialized using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Math.random(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>toString(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that interprets the result for you. In 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>main(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is filled with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
and their associated 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Prediction</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s.
The 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Hashtable
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
printed so you can see that it has been filled. Then a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Groundhog</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with an identity number of 3 is used to look up the prediction for 

⌨️ 快捷键说明

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