ArrayList<String> al = new ArrayList<String>();
final Iterator<FileInputStream> it = al.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { public boolean hasMoreElements() {
return it.hasNext();
} public FileInputStream nextElement() {
return it.next();
}
};
final Iterator<FileInputStream> it = al.iterator();
Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { public boolean hasMoreElements() {
return it.hasNext();
} public FileInputStream nextElement() {
return it.next();
}
};
1.hasMoreElements——判断之前创建的迭代器对象是否包含元素。
2.nextElement——返回迭代器中当前的对象,该对象是一个文件输入流。
final Iterator<FileInputStream> it = al.iterator(); //迭代
public interface Enumeration<E> {
/**
* Tests if this enumeration contains more elements.
*
* @return <code>true</code> if and only if this enumeration object
* contains at least one more element to provide;
* <code>false</code> otherwise.
*/
boolean hasMoreElements(); /**
* Returns the next element of this enumeration if this enumeration
* object has at least one more element to provide.
*
* @return the next element of this enumeration.
* @exception NoSuchElementException if no more elements exist.
*/
E nextElement();
}实现这个接口需要定义hasMoreElements和nextElement两个方法。
public interface Enumeration<E> {
/**
* Tests if this enumeration contains more elements.
*
* @return <code>true</code> if and only if this enumeration object
* contains at least one more element to provide;
* <code>false</code> otherwise.
*/
boolean hasMoreElements(); /**
* Returns the next element of this enumeration if this enumeration
* object has at least one more element to provide.
*
* @return the next element of this enumeration.
* @exception NoSuchElementException if no more elements exist.
*/
E nextElement();
}
我也比较好奇FileInputStream的Iterator的引用指向Iterator<String>的问题...
这行代码编译有问题啊,Iterator<FileInputStream>泛型类型String不一致
ArrayList<String> al = new ArrayList<String>();//生成String类型ArrayList的对象a1,final Iterator<FileInputStream> it = al.iterator();//调用Iterator ()对ArrayList进行遍历Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() ;
Enumeration是一个接口,实现 Enumeration 接口的对象,它生成一系列元素,一次生成一个。连续调用 nextElement 方法将返回一系列的连续元素。 hasMoreElements()和nextElement()是Enumeration的接口
hasMoreElements
boolean hasMoreElements()测试此枚举是否包含更多的元素。 返回:
当且仅当此枚举对象至少还包含一个可提供的元素时,才返回 true;否则返回 false。--------------------------------------------------------------------------------nextElement
E nextElement()如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。 返回:
此枚举的下一个元素。
抛出:
NoSuchElementException - 如果没有更多的元素存在。