匿名内部类接口

时间:2017-01-04 06:15:14

标签: java interface doubly-linked-list listiterator anonymous-inner-class

我有这个LinkedListDouble类,它有public ListIterator<T>listIterator()方法,我正在尝试执行接口ListIterator,因为它是匿名的内部类,我走的是正确的路径吗?我应该做些什么来制作{{ 1}} / public int nextIndex()工作? nextIndex方法返回后续调用next将返回的元素的索引,如果列表迭代器位于列表的末尾,则返回列表大小,而previousIndex方法返回后续返回的元素的索引调用previous,如果列表迭代器位于列表as it said here

的开头,则返回-1

这里是LinkedListDouble类

public int previousIndex()

这是应该执行接口ListIterator的方法,因为它是匿名内部类,到目前为止我尝试做的事情:

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;
    }

1 个答案:

答案 0 :(得分:0)

这是来自LinkedList的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()方法中递增/递减,这些方法已经处理了List大小问题,因此无需担心nextIndex()和previousIndex()方法中的大小问题。