当前位置: 代码迷 >> J2SE >> 这段代码是什么意思?解决方案
  详细解决方案

这段代码是什么意思?解决方案

热度:3918   发布时间:2013-02-25 00:00:00.0
这段代码是什么意思?
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();
}
};

------解决方案--------------------------------------------------------
在 Enumeration 的基础上,直接定义了一个 匿名类,为其增加了两个函数,仅此而已。
------解决方案--------------------------------------------------------
ArrayList<String> al = new ArrayList<String>();//定义了一个ArrayList集合对象并且规定了该集合里的元素类型为String型。

final Iterator<FileInputStream> it = al.iterator();//创建了一个al集合的迭代器。

下面一段代码声明了一个枚举型对象,并且重写了该对象中的两个方法:
1.hasMoreElements——判断之前创建的迭代器对象是否包含元素。
2.nextElement——返回迭代器中当前的对象,该对象是一个文件输入流。
------解决方案--------------------------------------------------------
匿名类,Enumeration<E>是一个接口。接口定义如下:

Java code
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();}
------解决方案--------------------------------------------------------
不知道楼上两位有没有编译过,先不看第一行 奇葩的写法,(正常人写都会声明成List接口),第二行FileInputStream的Iterator的引用怎么能够指向一个Iterator<String>的对象?

后面的代码是匿名类实现了Enumeration接口

Java code
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();}
------解决方案--------------------------------------------------------
探讨

不知道楼上两位有没有编译过,先不看第一行 奇葩的写法,(正常人写都会声明成List接口),第二行FileInputStream的Iterator的引用怎么能够指向一个Iterator<String>的对象?

后面的代码是匿名类实现了Enumeration接口

Java code

public interface Enumeration<E> {
/**
* Te……

------解决方案--------------------------------------------------------
探讨
引用:

不知道楼上两位有没有编译过,先不看第一行 奇葩的写法,(正常人写都会声明成List接口),第二行FileInputStream的Iterator的引用怎么能够指向一个Iterator<String>的对象?

后面的代码是匿名类实现了Enumeration接口

Java code

public interface Enumeration<E> {
/……

------解决方案--------------------------------------------------------
ArrayList<String> al = new ArrayList<String>();

final Iterator<FileInputStream> it = al.iterator();
这行代码编译有问题啊,Iterator<FileInputStream>泛型类型String不一致
------解决方案--------------------------------------------------------
探讨

ArrayList<String> al = new ArrayList<String>();
  相关解决方案