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

📄 tij0090.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
      <font color="#0000ff">else</font>
        bs.clear(i);
    System.out.println("<font color="#0000ff">short</font> value: " + st);
    printBitSet(bs);

    <font color="#0000ff">int</font> it = rand.nextInt();
    BitSet bi = <font color="#0000ff">new</font> BitSet();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 31; i &gt;=0; i--)
      <font color="#0000ff">if</font>(((1 &lt;&lt; i) &amp;  it) != 0)
        bi.set(i);
      <font color="#0000ff">else</font>
        bi.clear(i);
    System.out.println("<font color="#0000ff">int</font> value: " + it);
    printBitSet(bi);

    <font color="#009900">// Test bitsets &gt;= 64 bits:</font>
    BitSet b127 = <font color="#0000ff">new</font> BitSet();
    b127.set(127);
    System.out.println("set bit 127: " + b127);
    BitSet b255 = <font color="#0000ff">new</font> BitSet(65);
    b255.set(255);
    System.out.println("set bit 255: " + b255);
    BitSet b1023 = <font color="#0000ff">new</font> BitSet(512);
<font color="#009900">// Without the following, an exception is thrown</font>
<font color="#009900">// in the Java 1.0 implementation of BitSet:</font>
<font color="#009900">//    b1023.set(1023);</font>
    b1023.set(1024);
    System.out.println("set bit 1023: " + b1023);
  }
  <font color="#0000ff">static</font> <font color="#0000ff">void</font> printBitSet(BitSet b) {
    System.out.println("bits: " + b);
    String bbits = <font color="#0000ff">new</font> String();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> j = 0; j &lt; b.size() ; j++)
      bbits += (b.get(j) ? "1" : "0");
    System.out.println("bit pattern: " + bbits);
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
random number generator is used to create a random 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>byte</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>short</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>int</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and each one is transformed into a corresponding bit pattern in a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
This works fine because a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is 64 bits, so none of these cause it to increase in size. But in Java 1.0<A NAME="Index787"></A>,
when the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is greater than 64 bits, some strange behavior occurs. If you set a bit
that&#8217;s just one greater than the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">&#8217;s
currently-allocated storage, it will expand nicely. But if you try to set bits
at higher locations than that without first just touching the boundary,
you&#8217;ll get an exception, since the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
won&#8217;t expand properly in Java 1.0. The example shows a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
of 512 bits being created. The constructor allocates storage for twice that
number of bits. Then if you try to set bit 1024 or greater without first
setting bit 1023, you&#8217;ll throw an exception in <A NAME="Index788"></A>Java
1.0. Fortunately, this is fixed in Java 1.1,<A NAME="Index789"></A>
but avoid using the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>BitSet</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if you write code for Java 1.0.
</FONT><a name="_Toc375545356"></a><a name="_Toc408018572"></a><P></DIV>
<A NAME="Heading255"></A><H3 ALIGN=LEFT>
Stack</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
<A NAME="Index790"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is sometimes referred to as a &#8220;last-in, first-out&#8221; (LIFO)
collection. That is, whatever you &#8220;push&#8221; on the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
last is the first item you can &#8220;pop&#8221; out. Like all of the other
collections in Java, what you push and pop are 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Object</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s,
so you must cast what you pop.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">What&#8217;s
rather odd is that instead of using a <A NAME="Index791"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
as a building block to create a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">is
inherited from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
So it has all of the characteristics and behaviors of a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>plus</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
some extra 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
behaviors. It&#8217;s difficult to know whether the designers explicitly
decided that this was an especially useful way to do things, or whether it was
just a na&iuml;ve design.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Here&#8217;s
a simple demonstration of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
that reads each line from an array and pushes it as a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>String</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:</FONT><P></DIV>

<font color="#990000"><PRE><font color="#009900">//: Stacks.java</font>
<font color="#009900">// Demonstration of Stack Class</font>
<font color="#0000ff">import</font> java.util.*;

<font color="#0000ff">public</font> <font color="#0000ff">class</font> Stacks {
  <font color="#0000ff">static</font> String[] months = { 
    "January", "February", "March", "April",
    "May", "June", "July", "August", "September",
    "October", "November", "December" };
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Stack stk = <font color="#0000ff">new</font> Stack();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; months.length; i++)
      stk.push(months[i] + " ");
    System.out.println("stk = " + stk);
    <font color="#009900">// Treating a stack as a Vector:</font>
    stk.addElement("The last line");
    System.out.println(
      "element 5 = " + stk.elementAt(5));
    System.out.println("popping elements:");
    <font color="#0000ff">while</font>(!stk.empty())
      System.out.println(stk.pop());
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Each
line in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>months
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">array
is inserted into the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
with 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>push(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and later fetched from the top of the stack with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>pop(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
To make a point, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">operations
are also performed on the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object. This is possible because, by virtue of inheritance, a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>is</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Thus, all operations that can be performed on a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
can also be performed on a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
such as 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>elementAt(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><a name="_Toc375545357"></a><a name="_Toc408018573"></a><P></DIV>
<A NAME="Heading256"></A><H3 ALIGN=LEFT>
Hashtable<P><A NAME="Index792"></A></H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
allows you to select from a sequence of objects using a number, so in a sense
it associates numbers to objects. But what if you&#8217;d like to select from a
sequence of objects using some other criterion? A 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Stack</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is an example: its selection criterion is &#8220;the last thing pushed on the
stack.&#8221; A powerful twist on this idea of &#8220;selecting from a
sequence&#8221; is alternately termed a <A NAME="Index793"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>map</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
a <A NAME="Index794"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>dictionary,</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or an <A NAME="Index795"></A><A NAME="Index796"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>associative
array
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Conceptually, it seems like a vector, but instead of looking up objects using a
number, you look them up using 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>another
object
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">!
This is often a key process in a program.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
concept shows up in Java as the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>abstract
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">class</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>
Dictionary
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
The interface for this class is straightforward: 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>size(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
tells you how many elements are within, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>isEmpty(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>true</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
if there are no elements, 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>put(Object
key, Object value)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
adds a value (the thing you want), and associates it with a key (the thing you
look it up with). 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>get(Object
key)
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">

⌨️ 快捷键说明

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