按照书写了一个程序。提示说没实现hasNext()函数,各位高手帮帮忙
package test1;import java.util.*;public class LinkedList<T> implements Iterator<T> {

private ListItem start = null;
private ListItem end = null;
private ListItem current = null;

public Iterator<T> iterator(){
return new ListIterator();
}

public LinkedList(){}

public LinkedList(T item){
if(item !=null){
current = end = start = new ListItem(item);
}
}

public LinkedList(T[] items){
if(items!=null){
for(int i=0;i<items.length;i++){
addItem(items[i]);
}
}
}

public void addItem(T item){
ListItem newEnd = new ListItem(item);
if(start==null){
start = end = newEnd;
}else{
end.next = newEnd;
end = newEnd;
}
}

public T getFirst(){
current = start;
return start ==null?null:start.item;
}

public T getNext(){
if(current!=null){
current = current.next;
}
return current == null?null:current.item;
}

private class ListItem{
 ListItem next;
 T item;
public ListItem(T item){
this.item = item;
next = null;
}

public String toString(){
return "ListItem"+item;
}
}

private class ListIterator implements Iterator<T>{

private T nextElement;
public ListIterator(){
nextElement = getFirst();
}

public boolean hasNext(){
return nextElement!=null;
}

public T next(){
T element = nextElement;
if(element==null){
throw new NoSuchElementException();
}
nextElement = getNext();
return element;
}

public void remove(){
throw new IllegalStateException();
}
}
}

解决方案 »

  1.   

    自己写一个吧
    @Override
    public boolean hasNext()
    {
    // TODO Auto-generated method stub
    return false;
    }
      

  2.   

    那本书那么厉害,拿Iterator开刷
      

  3.   

    LZ 搞错了,public class LinkedList<T> implements Iterator<T> {应该是 Iterable
      

  4.   

    谢谢各位。刚学的。看到Iterable<>和Iterator<>差不多。所以就弄混了。