我如何在 Iterator<E> 的 "next" 方法中使用 return?
How I have to return in "next" method of Iterator<E>?
在 Iterator 中实现的方法 "next" 有一个错误。我不能 return 'e' 值,因为 Java 说它是对象类型。我不明白为什么。因为在 Elem 中 class 如果我给例如 Point 对象,它必须 return 它。但我不太了解泛型,也许有人可以解释一下。谢谢)
package Tutorial3;
import java.util.Iterator;
public class MyLinkedList<E> implements Iterable<E> {
Elem<E> head;
Elem<E> tail;
public MyLinkedList() {
head = null;
tail = null;
}
public void add(E e) {
Elem<E> newElem = new Elem<E>(e);
if (head == null) {
tail.setNext(newElem);
head = newElem;
tail = newElem;
} else {
tail.setNext(newElem);
tail = newElem;
}
}
public void addToHead(E e) {
Elem<E> newElem = new Elem<E>(e);
newElem.setNext(head);
head = newElem;
}
public void addToTail(E e) {
add(e);
}
public void removeFirstValue() {
head = head.getNext();
}
public void removeLastValue() {
Elem<E> cursor;
cursor = head;
while (cursor.getNext() != tail) {
cursor = cursor.getNext();
}
tail = cursor;
tail.setNext(null);
}
public E get(int index) {
if ((index + 1) > size()) {
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size());
} else {
Elem<E> cursor;
cursor = head;
int i = 0;
if (i == index) {
return cursor.getE();
} else {
while (i != index) {
cursor = cursor.getNext();
i++;
}
return cursor.getE();
}
}
}
public int size() {
Elem<E> cursor;
cursor = head;
int size = 0;
while (cursor != null) {
size++;
cursor = cursor.getNext();
}
return size;
}
@Override
public Iterator<E> iterator() {
return new MyLinkedListIterator<E>();
}
private class MyLinkedListIterator<T> implements Iterator<T> {
private Elem elem = head;
@Override
public boolean hasNext() {
return elem.getNext() != null;
}
@Override
public T next() {
return elem.getNext().getE(); //here is mistake: can't return e
//required : T, Found: Object
}
}
}
package Tutorial3;
public class Elem<E> {
private E e;
private Elem<E> next;
public Elem(E e) {
this.e = e;
}
public Elem(E e, Elem<E> next) {
this.e = e;
this.next = next;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public Elem<E> getNext() {
return next;
}
public void setNext(Elem<E> next) {
this.next = next;
}
}
应该是
private class MyLinkedListIterator implements Iterator<E> {
private Elem<E> elem = head;
@Override
public boolean hasNext() {
return elem.getNext() != null;
}
@Override
public E next() {
return elem.getNext().getE();
}
}
您的 MyLinkedListIterator
类型应该从外部 MyLinkedList<E>
类型继承 E
,而不是声明它自己的元素类型 T
,然后您通过使用原始 Elem
类型而不是 Elem<E>
.
在 Iterator 中实现的方法 "next" 有一个错误。我不能 return 'e' 值,因为 Java 说它是对象类型。我不明白为什么。因为在 Elem 中 class 如果我给例如 Point 对象,它必须 return 它。但我不太了解泛型,也许有人可以解释一下。谢谢)
package Tutorial3;
import java.util.Iterator;
public class MyLinkedList<E> implements Iterable<E> {
Elem<E> head;
Elem<E> tail;
public MyLinkedList() {
head = null;
tail = null;
}
public void add(E e) {
Elem<E> newElem = new Elem<E>(e);
if (head == null) {
tail.setNext(newElem);
head = newElem;
tail = newElem;
} else {
tail.setNext(newElem);
tail = newElem;
}
}
public void addToHead(E e) {
Elem<E> newElem = new Elem<E>(e);
newElem.setNext(head);
head = newElem;
}
public void addToTail(E e) {
add(e);
}
public void removeFirstValue() {
head = head.getNext();
}
public void removeLastValue() {
Elem<E> cursor;
cursor = head;
while (cursor.getNext() != tail) {
cursor = cursor.getNext();
}
tail = cursor;
tail.setNext(null);
}
public E get(int index) {
if ((index + 1) > size()) {
throw new IndexOutOfBoundsException("Index: " + index + " Size: " + size());
} else {
Elem<E> cursor;
cursor = head;
int i = 0;
if (i == index) {
return cursor.getE();
} else {
while (i != index) {
cursor = cursor.getNext();
i++;
}
return cursor.getE();
}
}
}
public int size() {
Elem<E> cursor;
cursor = head;
int size = 0;
while (cursor != null) {
size++;
cursor = cursor.getNext();
}
return size;
}
@Override
public Iterator<E> iterator() {
return new MyLinkedListIterator<E>();
}
private class MyLinkedListIterator<T> implements Iterator<T> {
private Elem elem = head;
@Override
public boolean hasNext() {
return elem.getNext() != null;
}
@Override
public T next() {
return elem.getNext().getE(); //here is mistake: can't return e
//required : T, Found: Object
}
}
}
package Tutorial3;
public class Elem<E> {
private E e;
private Elem<E> next;
public Elem(E e) {
this.e = e;
}
public Elem(E e, Elem<E> next) {
this.e = e;
this.next = next;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
public Elem<E> getNext() {
return next;
}
public void setNext(Elem<E> next) {
this.next = next;
}
}
应该是
private class MyLinkedListIterator implements Iterator<E> {
private Elem<E> elem = head;
@Override
public boolean hasNext() {
return elem.getNext() != null;
}
@Override
public E next() {
return elem.getNext().getE();
}
}
您的 MyLinkedListIterator
类型应该从外部 MyLinkedList<E>
类型继承 E
,而不是声明它自己的元素类型 T
,然后您通过使用原始 Elem
类型而不是 Elem<E>
.