英文:
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<E> implements Iterator<E>
, not
public class PeekingIterator implements Iterator<E>
. I ask this because if I replace E with Integer in the code, it will work. I mean public class PeekingIterator implements Iterator<Integer>
is correct.
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;
}
}
答案1
得分: 4
E
是一个标识符。如果你声明 class PeekingIterator<E> implements Iterator<E>
,编译器就知道这个标识符是一个泛型类型参数。
这使你能够实例化 PeekingIterator
如下:
PeekingIterator<Integer> it1 = new PeekingIterator<>();
或者
PeekingIterator<String> it2 = new PeekingIterator<>();
也就是说,你的 PeekingIterator
类可以迭代任何类型的元素。
如果你将 PeekingIterator
声明为 class PeekingIterator implements Iterator<E>
,编译器会查找名为 E
的某种类型(类或接口)。如果找不到,就会导致编译错误。
如果你将 class PeekingIterator implements Iterator<Integer>
这样声明,这是有效的,因为 Integer
是一个类。这会限制你的 PeekingIterator
始终只能迭代 Integer
元素。
英文:
E
is an identifier. If you say that class PeekingIterator<E> implements Iterator<E>
, the compiler knows that this identifier is a generic type parameter.
This allows you to instantiate PeekingIterator
as:
PeekingIterator<Integer> it1 = new PeekingIterator<>();
or
PeekingIterator<String> it2 = new PeekingIterator<>();
i.e. your PeekingIterator
class can iterate over any type of elements.
If you declare PeekingIterator
as class PeekingIterator implements Iterator<E>
, 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<Integer>
, this works, since Integer
is a class. This limits your PeekingIterator
to always iterate over Integer
elements.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论