首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-09-19 
这段代码是什么意思?ArrayListString al new ArrayListString()final IteratorFileInputStream i

这段代码是什么意思?
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>();



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


[解决办法]
[Quote=引用楼主 的回复
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 - 如果没有更多的元素存在。
[解决办法]
借尸还魂大法而已

热点排行