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

📄 tij0093.html

📁 学习java的经典书籍
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<font color="#0000ff">public</font> <font color="#0000ff">class</font> SimpleCollection {
  <font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">void</font> main(String[] args) {
    Collection c = <font color="#0000ff">new</font> ArrayList();
    <font color="#0000ff">for</font>(<font color="#0000ff">int</font> i = 0; i &lt; 10; i++)
      c.add(Integer.toString(i));
    Iterator it = c.iterator();
    <font color="#0000ff">while</font>(it.hasNext())
      System.out.println(it.next());
  }
} <font color="#009900">///:~ </PRE></font></font><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
the code examples for the new collections libraries will be placed in the
subdirectory 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>newcollections</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
so you&#8217;ll be reminded that these work only with Java 1.2. As a result,
you must invoke the program by saying:
</FONT><P></DIV><DIV ALIGN=LEFT><TT><FONT FACE="Courier New" SIZE=3 COLOR="Black">java
c08.newcollections.SimpleCollection
</FONT></TT><P></DIV><DIV ALIGN=LEFT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">with
a similar syntax for the rest of the programs in the package.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">You
can see that the new collections are part of the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>java.util</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
library, so you don&#8217;t need to add any extra 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>import</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
statements to use them.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
first line 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">
creates an <A NAME="Index844"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ArrayList</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
object and then upcasts it to a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
Since this example uses only the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection
</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">methods,
any object of a class inherited from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
would work, but 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ArrayList</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is the typical workhorse 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and takes the place of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Vector</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method, as its name suggests, puts a new element in the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
However, the documentation carefully states that 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
&#8220;ensures that this Collection contains the specified element.&#8221; This
is to allow for the meaning of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which adds the element only if it isn&#8217;t already there. With an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>ArrayList</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
or any sort of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>add(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
always means &#8220;put it in.&#8221;
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
can produce an <A NAME="Index845"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
via their <A NAME="Index846"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>iterator(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
method. An 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is just like an <A NAME="Index847"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which it replaces, except:
</FONT><P></DIV>
<OL>
<LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	It
uses a name (iterator) that is historically understood and accepted in the OOP
community.
</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	It
uses shorter method names than 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Enumeration</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">:
<A NAME="Index848"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hasNext(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>hasMoreElements(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and <A NAME="Index849"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>next(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
instead of 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>nextElement(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT><LI><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">	It
adds a new method, <A NAME="Index850"></A></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>remove(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
which removes the last element produced by the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
So you can call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>remove(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
only once for every time you call 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>next(&#160;)</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.</FONT></OL><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">In
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>SimpleCollection.java</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
you can see that an 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
is created and used to traverse the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
printing each element.
</FONT><a name="_Toc408018578"></a><P></DIV>
<A NAME="Heading263"></A><H3 ALIGN=LEFT>
Using
Collections
</H3>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">The
following table shows everything you can do with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and thus, everything you can do with a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Set</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
or a 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List.</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
(
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>List</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
also has additional functionality.) 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Map</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">s
are not inherited from 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Collection</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">,
and will be treated separately.
</FONT><P></DIV>
<DIV ALIGN=LEFT><TABLE BORDER>
<COLGROUP>
      <COL width="126">
      <COL width="216">
</COLGROUP>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
add(Object)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*Ensures
that the Collection contains the argument. Returns false if it doesn&#8217;t
add the argument.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
addAll(Collection)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*Adds
all the elements in the argument. Returns true if any elements were added.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>void
clear(&#160;)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*Removes
all the elements in the Collection. 
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
contains(Object)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">True
if the Collection contains the argument. 
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
containsAll(Collection)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">True
if the Collection contains all the elements in the argument. 
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
isEmpty(&#160;)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">True
if the Collection has no elements. 
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>Iterator
iterator(&#160;)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Returns
an Iterator that you can use to move through the elements in the Collection. 
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD WIDTH=126 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>boolean
remove(Object)
</B></FONT><P></DIV>
</TD>
<TD WIDTH=216 COLSPAN=1 ROWSPAN=1 VALIGN=TOP>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">*If
the argument is in the Collection, one instance of that element is removed.
Returns true if a removal occurred.
</FONT><P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">

⌨️ 快捷键说明

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