📄 _chapter 2.htm
字号:
you can advance the iterator to the next position, just like you can advance an
array index with the <tt>i++</tt> operation without performing a lookup.
However, the Java iterators do not work like that. The lookup and position
change are tightly coupled. The only way to look up an element is to call <tt>
next</tt>, and that lookup advances the position.</p>
<p class="docText">Instead, you should think of Java iterators as being
<span class="docEmphasis">between elements.</span> When you call <tt>next</tt>,
the iterator <span class="docEmphasis">jumps over</span> the next element, and
it returns a reference to the element that it just passed (see
<a class="docLink" href="#ch02fig03">Figure 2-3</a>).</p>
<center>
<h5 id="ch02fig03" class="docFigureTitle">Figure 2-3. Advancing an iterator</h5>
<p>
<img alt="graphics/02fig03.gif" src="02fig03.gif" border="0" width="500" height="346"></p>
</center>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">Here is another useful analogy. You can think of <tt>
Iterator.next</tt> as the equivalent of <tt>InputStream.read</tt>. Reading
a byte from a stream automatically "consumes" the byte. The next call to
<tt>read</tt> consumes and returns the next byte from the input.
Similarly, repeated calls to <tt>next</tt> let you read all elements in a
collection.</td>
</tr>
</table>
</div>
<p class="docText">You must be careful when using the <tt>remove</tt> method.
Calling <tt>remove</tt> removes the element that was returned by the last call
to <tt>next</tt>. That makes sense if you want to remove a particular value梱ou
need to see the element before you can decide that it is the one that should be
removed. But if you want to remove an element by position, you first need to
skip past the element. For example, here is how you remove the first element in
a collection.</p>
<pre>Iterator it = c.iterator();
it.next(); // skip over the first element
it.remove(); // now remove it
</pre>
<p class="docText">More importantly, there is a dependency between calls to the
<tt>next</tt> and <tt>remove</tt> methods. It is illegal to call <tt>remove</tt>
if it wasn't preceded by a call to <tt>next</tt>. If you try, an <tt>
IllegalStateException</tt> is thrown.</p>
<p class="docText">If you want to remove two adjacent elements, you cannot
simply call:</p>
<pre>it.remove();
it.remove(); // Error!
</pre>
<p class="docText">Instead, you must first call <tt>next</tt> to jump over the
element to be removed.</p>
<pre>it.remove();
it.next();
it.remove(); // Ok
</pre>
<p class="docText">Because the collection and iterator interfaces are generic,
you can write utility methods that operate on any kind of collection. For
example, here is a generic <tt>print</tt> method that prints all elements in a
collection.</p>
<pre>public static void print(Collection c)
{
System.out.print("[ ");
Iterator iter = c.iterator();
while (iter.hasNext())
System.out.print(iter.next() + " ");
System.out.println("]");
}
</pre>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">We give this example to illustrate how to write a
generic method. If you want to print the elements in a collection, you can
just call <tt>System.out.println(c)</tt>. This works because each
collection class has a <tt>toString</tt> method that returns a string
containing all elements in the collection.</td>
</tr>
</table>
</div>
<p class="docText">Here is a method that adds all objects from one collection to
another:</p>
<pre>public static boolean addAll(Collection to, Collection from)
{
Iterator iter = from.iterator();
boolean modified = false;
while (iter.hasNext())
if (to.add(iter.next()))
modified = true;
return modified;
}
</pre>
<p class="docText">Recall that the <tt>add</tt> method returns <tt>true</tt> if
adding the element modified the collection. You can implement these utility
methods for arbitrary collections because the <tt>Collection</tt> and <tt>
Iterator</tt> interfaces supply fundamental methods such as <tt>add</tt> and <tt>
next</tt>.</p>
<p class="docText">The designers of the Java library decided that some of these
utility methods are so useful that the library should make them available. That
way, users don't have to keep reinventing the wheel. The <tt>addAll</tt> method
is one such method.</p>
<p class="docText">Had <tt>Collection</tt> been an abstract class instead of an
interface, then it would have been an easy matter to supply this functionality
in the class. However, you cannot supply methods in interfaces of the Java
programming language. Therefore, the collection library takes a slightly
different approach. The <tt>Collection</tt> interface declares quite a few
useful methods that all implementing classes must supply. Among them are:</p>
<pre>int size()
boolean isEmpty()
boolean contains(Object obj)
boolean containsAll(Collection c)
boolean equals(Object other)
boolean addAll(Collection from)
boolean remove(Object obj)
boolean removeAll(Collection c)
void clear()
boolean retainAll(Collection c)
Object[] toArray()
</pre>
<p class="docText">Many of these methods are self-explanatory; you will find
full documentation in the API notes at the end of this section.</p>
<p class="docText">Of course, it is a bother if every class that implements the
<tt>Collection</tt> interface has to supply so many routine methods. To make
life easier for implementors, the class <tt>AbstractCollection</tt> leaves the
fundamental methods (such as <tt>add</tt> and <tt>iterator</tt>) abstract but
implements the routine methods in terms of them. For example:</p>
<pre>public class AbstractCollection
implements Collection
{
. . .
public abstract boolean add(Object obj);
public boolean addAll(Collection from)
{
Iterator iter = from.iterator();
boolean modified = false;
while (iter.hasNext())
if (add(iter.next()))
modified = true;
return modified;
}
. . .
}
</pre>
<p class="docText">A concrete collection class can now extend the <tt>
AbstractCollection</tt> class. It is now up to the concrete collection class to
supply an <tt>add</tt> method, but the <tt>addAll</tt> method has been taken
care of by the <tt>AbstractCollection</tt> superclass. However, if the subclass
has a more efficient way of implementing <tt>addAll</tt>, it is free to do so.</p>
<p class="docText">This is a good design for a class framework. The users of the
collection classes have a richer set of methods available in the generic
interface, but the implementors of the actual data structures do not have the
burden of implementing all the routine methods.</p>
<h5 class="docSection3Title" id="ch02lev3sec1"><span class="docEmphasis"><tt>java.util.Collection</tt></span></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>Iterator iterator()</tt></p>
<p class="docList">returns an iterator that can be used to visit the elements
in the collection.</li>
<li>
<p class="docList"><tt>int size()</tt></p>
<p class="docList">returns the number of elements currently stored in the
collection.</li>
<li>
<p class="docList"><tt>boolean isEmpty()</tt></p>
<p class="docList">returns <tt>true</tt> if this collection contains no
elements.</li>
<li>
<p class="docList"><tt>boolean contains(Object obj)</tt></p>
<p class="docList">returns <tt>true</tt> if this collection contains an object
equal to <tt>obj</tt>.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>obj</tt></td>
<td class="docTableCell" vAlign="top">the object to match in the
collection</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>boolean containsAll(Collection other)</tt></p>
<p class="docList">returns <tt>true</tt> if this collection contains all
elements in the other collection.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>other</tt></td>
<td class="docTableCell" vAlign="top">the collection holding the elements
to match</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>boolean add(Object element)</tt></p>
<p class="docList">adds an element to the collection. Returns <tt>true</tt> if
the collection changed as a result of this call.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>element</tt></td>
<td class="docTableCell" vAlign="top">the element to add</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>boolean addAll(Collection other)</tt></p>
<p class="docList">adds all elements from the other collection to this
collection. Returns <tt>true</tt> if the collection changed as a result of
this call.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>other</tt></td>
<td class="docTableCell" vAlign="top">the collection holding the elements
to add</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>boolean remove(Object obj)</tt></p>
<p class="docList">removes an object equal to <tt>obj</tt> from this
collection. Returns <tt>true</tt> if a matching object was removed.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>obj</tt></td>
<td class="docTableCell" vAlign="top">an object that equals the element to
remove</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>boolean removeAll(Collection other)</tt></p>
<p class="docList">removes all elements from the other collection from this
collection. Returns <tt>true</tt> if the collection changed as a result of
this call.</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -