为什么在我实现Iterator<E>时,Java 泛型类型是必需的。

huangapple go评论67阅读模式
英文:

why the java generic type is a must when I implement Interator<E>

问题

以下是翻译好的部分:

我写了这段代码它运行良好但我有一个问题

为什么它应该是 ```public class PeekingIterator<E> implements Iterator<E>```,而不是
```public class PeekingIterator implements Iterator<E>```。我之所以问这个问题是因为如果我在代码中将 E 替换为 Integer它也能工作我的意思是 ```public class PeekingIterator implements Iterator<Integer>``` 是正确的

import java.util.Iterator;

public class PeekingIterator<E> implements Iterator<E> {

    private E nextElem;
    private boolean hasNextElem;
    private Iterator<E> iter;

    public PeekingIterator(Iterator<E> iterator) {
        iter = iterator;
        nextElem = next();
        hasNextElem = true;
    }

    public E peek() {
        return nextElem;
    }

    @Override
    public E next() {
        if (!hasNextElem) {
            throw new RuntimeException();
        }
        E res = nextElem;
        if (hasNext()) {
            nextElem = iter.next();
            hasNextElem = true;
        } else {
            hasNextElem = false;
        }
        return res;
    }

    @Override
    public boolean hasNext() {
        return hasNextElem;
    }
}
英文:

I wrote this piece of code, and it works fine. but I have a question:

why it should be public class PeekingIterator&lt;E&gt; implements Iterator&lt;E&gt;, not
public class PeekingIterator implements Iterator&lt;E&gt;. I ask this because if I replace E with Integer in the code, it will work. I mean public class PeekingIterator implements Iterator&lt;Integer&gt; is correct.

import java.util.Iterator;

public class PeekingIterator&lt;E&gt; implements Iterator&lt;E&gt; {

    private E nextElem;
    private boolean hasNextElem;
    private Iterator&lt;E&gt; iter;

    public PeekingIterator(Iterator&lt;E&gt; iterator) {
        iter = iterator;
        nextElem = next();
        hasNextElem = true;

    }

    public E peek() {
        return nextElem;
    }

    @Override
    public E next() {
        if (!hasNextElem) {
            throw new RuntimeException();
        }
        E res = nextElem;
        if (hasNext()) {
            nextElem = iter.next();
            hasNextElem = true;
        } else {
            hasNextElem = false;
        }
        return res;

    }

    @Override
    public boolean hasNext() {
        return hasNextElem;
    }

}

答案1

得分: 4

E 是一个标识符。如果你声明 class PeekingIterator&lt;E&gt; implements Iterator&lt;E&gt;,编译器就知道这个标识符是一个泛型类型参数。

这使你能够实例化 PeekingIterator 如下:

PeekingIterator&lt;Integer&gt; it1 = new PeekingIterator&lt;&gt;();

或者

PeekingIterator&lt;String&gt; it2 = new PeekingIterator&lt;&gt;();

也就是说,你的 PeekingIterator 类可以迭代任何类型的元素。

如果你将 PeekingIterator 声明为 class PeekingIterator implements Iterator&lt;E&gt;,编译器会查找名为 E 的某种类型(类或接口)。如果找不到,就会导致编译错误。

如果你将 class PeekingIterator implements Iterator&lt;Integer&gt; 这样声明,这是有效的,因为 Integer 是一个类。这会限制你的 PeekingIterator 始终只能迭代 Integer 元素。

英文:

E is an identifier. If you say that class PeekingIterator&lt;E&gt; implements Iterator&lt;E&gt;, the compiler knows that this identifier is a generic type parameter.

This allows you to instantiate PeekingIterator as:

PeekingIterator&lt;Integer&gt; it1 = new PeekingIterator&lt;&gt;();

or

PeekingIterator&lt;String&gt; it2 = new PeekingIterator&lt;&gt;();

i.e. your PeekingIterator class can iterate over any type of elements.

If you declare PeekingIterator as class PeekingIterator implements Iterator&lt;E&gt;, the compiler searches for some type (class or interface) named E. If it doesn't find it, that's a compilation error.

If you declare class PeekingIterator implements Iterator&lt;Integer&gt;, this works, since Integer is a class. This limits your PeekingIterator to always iterate over Integer elements.

huangapple
  • 本文由 发表于 2020年10月7日 12:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64237357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定