📄 iteratorexpression.java
字号:
+ "s beyond the end of the array. ");
}
if (bound < 0) {
throw new ArrayIndexOutOfBoundsException(
"Attempt to make an ArrayIterator that " + type
+ "s before the start of the array. ");
}
}
// Iterator interface
//-----------------------------------------------------------------------
/**
* Returns true if there are more elements to return from the array.
*
* @return true if there is a next element to return
*/
public boolean hasNext() {
return (index < endIndex);
}
/**
* Returns the next element in the array.
*
* @return the next element in the array
* @throws NoSuchElementException
* if all the elements in the array have already been
* returned
*/
public Object next() {
if (hasNext() == false) {
throw new NoSuchElementException();
}
return Array.get(array, index++);
}
/**
* Throws {@link UnsupportedOperationException}.
*
* @throws UnsupportedOperationException
* always
*/
public void remove() {
throw new UnsupportedOperationException(
"remove() method is not supported");
}
// Properties
//-----------------------------------------------------------------------
/**
* Gets the array that this iterator is iterating over.
*
* @return the array this iterator iterates over, or <code>null</code>
* if the no-arg constructor was used and
* {@link #setArray(Object)}has never been called with a valid
* array.
*/
public Object getArray() {
return array;
}
/**
* Sets the array that the ArrayIterator should iterate over.
* <p>
* If an array has previously been set (using the single-arg constructor
* or this method) then that array is discarded in favour of this one.
* Iteration is restarted at the start of the new array. Although this
* can be used to reset iteration, the {@link #reset()}method is a more
* effective choice.
*
* @param array
* the array that the iterator should iterate over.
* @throws IllegalArgumentException
* if <code>array</code> is not an array.
* @throws NullPointerException
* if <code>array</code> is <code>null</code>
*/
public void setArray(final Object array) {
// Array.getLength throws IllegalArgumentException if the object is
// not
// an array or NullPointerException if the object is null. This call
// is made before saving the array and resetting the index so that
// the
// array iterator remains in a consistent state if the argument is
// not
// an array or is null.
this.endIndex = Array.getLength(array);
this.startIndex = 0;
this.array = array;
this.index = 0;
}
/**
* Resets the iterator back to the start index.
*/
public void reset() {
this.index = this.startIndex;
}
}
/**
* Adapter to make {@link Enumeration Enumeration}instances appear to be
* {@link Iterator Iterator}instances. Originated in commons-collections.
* Added as a private inner class to break dependency.
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall </a>
*/
private static final class EnumerationIterator implements Iterator {
/** The collection to remove elements from */
private Collection collection;
/** The enumeration being converted */
private Enumeration enumeration;
/** The last object retrieved */
private Object last;
// Constructors
//-----------------------------------------------------------------------
/**
* Constructs a new <code>EnumerationIterator</code> that will not
* function until {@link #setEnumeration(Enumeration)} is called.
*/
public EnumerationIterator() {
this(null, null);
}
/**
* Constructs a new <code>EnumerationIterator</code> that provides
* an iterator view of the given enumeration.
*
* @param enumeration the enumeration to use
*/
public EnumerationIterator(final Enumeration enumeration) {
this(enumeration, null);
}
/**
* Constructs a new <code>EnumerationIterator</code> that will remove
* elements from the specified collection.
*
* @param enumeration the enumeration to use
* @param collection the collection to remove elements form
*/
public EnumerationIterator(final Enumeration enumeration,
final Collection collection) {
super();
this.enumeration = enumeration;
this.collection = collection;
this.last = null;
}
// Iterator interface
//-----------------------------------------------------------------------
/**
* Returns true if the underlying enumeration has more elements.
*
* @return true if the underlying enumeration has more elements
* @throws NullPointerException if the underlying enumeration is null
*/
public boolean hasNext() {
return enumeration.hasMoreElements();
}
/**
* Returns the next object from the enumeration.
*
* @return the next object from the enumeration
* @throws NullPointerException if the enumeration is null
*/
public Object next() {
last = enumeration.nextElement();
return last;
}
/**
* Removes the last retrieved element if a collection is attached.
* <p>
* Functions if an associated <code>Collection</code> is known.
* If so, the first occurrence of the last returned object from this
* iterator will be removed from the collection.
*
* @exception IllegalStateException <code>next()</code> not called.
* @exception UnsupportedOperationException if no associated collection
*/
public void remove() {
if (collection != null) {
if (last != null) {
collection.remove(last);
} else {
throw new IllegalStateException(
"next() must have been called for remove() to function");
}
} else {
throw new UnsupportedOperationException(
"No Collection associated with this Iterator");
}
}
// Properties
//-----------------------------------------------------------------------
/**
* Returns the underlying enumeration.
*
* @return the underlying enumeration
*/
public Enumeration getEnumeration() {
return enumeration;
}
/**
* Sets the underlying enumeration.
*
* @param enumeration the new underlying enumeration
*/
public void setEnumeration(final Enumeration enumeration) {
this.enumeration = enumeration;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -