0

我有這LinkedListDouble類有public ListIterator<T>listIterator()方法,我試圖執行接口ListIterator,因爲它是匿名內部類,我走的是正確的道路?我該怎麼做才能使public int nextIndex()/public int previousIndex()工作? nextIndex方法返回後續調用返回的元素索引,如果列表迭代器位於列表末尾,並且previousIndex方法返回將由a返回的元素的索引後續調用以前,或-1,如果列表迭代器在列表as it said here匿名內部類接口

這裏開始就是LinkedListDouble類

public class LinkedListDouble <T> { 
private Node first = null; 
private Node last = null; 

public LinkedListDouble()   // constructor 
{ 
    first = null;     // no items on list yet 
    last = null; 

} 

public void add(T item) { 
    Node newNode = new Node(item); 
     if (isEmpty()) { 
      first =newNode; 
      last = newNode; 

     } 
     else { 
      //first.setPrev(newNode); 
      //newNode.setNext(first); 
      //first = newNode; 

      last.setNext(newNode); 
      newNode.setPrev(last); 
      last=newNode; 
     } 
    } 
    public boolean contains(T item){ 
     if(first==null) 
      return false; 
     else 
     { 
      Node newNode=first; 
      while(newNode!=null) 
      { 
       if(newNode.getInfo().equals(item)) 
        return true; 
       else 
        newNode=newNode.getNext(); 
      } 
     } 
     return false; 
} 

    public T remove(T item) 
    {//get care of first and last nodes 
     //and if there is more than 1 matching 
     boolean check=contains(item); 
     if(check==true) 

     { 
      Node newNode=first; 
      while(newNode!=null) 
      { 
       if(newNode.getInfo().equals(item)) 
       { 
        newNode.getPrev().setNext(newNode.getNext()); 
        newNode.getNext().setPrev(newNode.getPrev()); 
        return item; 
       } 
       else 
        newNode=newNode.getNext(); 
      } 

     } 
     return null; 

    } 

    public int size() 
    { 
     int size=0; 
     if(first==null) 
      return size; 
     else 
     { 

     Node newNode=first; 
     while(newNode!=null) 
     { 
      size++; 
      newNode=newNode.getNext(); 

     } 

     } 
     return size; 
    } 

    public String toString() 
    { 
    Node newNode=first; 
    String s=""; 
    while(newNode!=null) 
    { 
     s+=newNode.getInfo()+" ,"; 
     newNode=newNode.getNext(); 
    } 
    return s; 

    } 

public boolean isEmpty() { 
     return first == null; 
    } 

,並在這裏是因爲它是匿名的應執行該接口的ListIterator方法內心階層和我到目前爲止所做的努力:

public ListIterator<T>listIterator() 
{ 
    ListIterator<T>listIterator = new ListIterator<T>() { 
    private Node current = first; 
    private Node temp2 = null; 
    private int curindex = 0; 

    @Override 
    public void add(T e) { 
     // TODO Auto-generated method stub 
     throw new RuntimeException(); 
    } 

    @Override 
    public boolean hasNext() { 
     // TODO Auto-generated method stub 
     boolean flag=true; 
     if(current.getNext()==null) 
     { 
      flag=false; 
     } 
     return flag; 
    } 

    @Override 
    public boolean hasPrevious() { 
     // TODO Auto-generated method stub 
     boolean flag=true; 
     if(current.getPrev()==null) 
     { 
      flag=false; 
     } 
     return flag; 
    } 

    @Override 
    public T next() { 
     // TODO Auto-generated method stub 
     if (!hasNext()) throw new NoSuchElementException(); 


     temp2=current.getNext(); 
      current=current.getNext(); 
      return (T) temp2.getInfo(); 



    } 

    @Override 
    public int nextIndex() { 
     // TODO Auto-generated method stub 
     int counter=0; 
     if(!hasNext()) return size(); 
     return curindex; 


    } 

    @Override 
    public T previous() { 
     // TODO Auto-generated method stub 
     if (!hasPrevious()) throw new NoSuchElementException(); 

     temp2 = current.getPrev(); 
     temp2 = temp2.getPrev(); 


      return (T) temp2.getInfo(); 
    } 

    @Override 
    public int previousIndex() { 
     // TODO Auto-generated method stub 
     int counter=0; 
     if(!hasPrevious()) return -1; 
     return curindex-1; 
    } 

    @Override 
    public void remove() { 
     // TODO Auto-generated method stub 
     throw new RuntimeException(); 
    } 

    @Override 
    public void set(T e) { 
     // TODO Auto-generated method stub 
     throw new RuntimeException(); 
    } 


    }; 
    return listIterator; 
} 

回答

0

這是從一個的ListIterator代碼示例(鏈表的)方法:

public E next() { 
    //check for modification 
    if (!hasNext()) 
     throw new NoSuchElementException(); 
    lastReturned = next; 
    next = next.next; 
    nextIndex++; 
    return lastReturned.item; 
} 


public E previous() { 
    //check for modification 
    if (!hasPrevious()) 
     throw new NoSuchElementException(); 
    lastReturned = next = (next == null) ? last : next.prev; 
    nextIndex--; 
    return lastReturned.item; 
} 

public int nextIndex() { 
    return nextIndex; 
} 

public int previousIndex() { 
    return nextIndex - 1; 
} 

正如你可以看到,需要保持像index的變量,以保持下一個索引的軌道。

下一個索引在next()和previous()方法中增加/減少,這些方法已經處理列表大小問題,因此不需要擔心nextIndex()和previousIndex()方法中的大小問題。

+0

謝謝你的幫助很多 – Liana