2017-03-05 90 views
1

我很接近完成我的项目,但我被困在一个部分。我试图从我的文件MyLinkedList.java中的一个名为AbstractLinkedList.java的文件中实现抽象方法。MyLinkedList.java项目问题

这里有两个项目文件:

AbstractLinkedList.java

/* 
Models a doubly-linked list with dummy header and tail. 
You should extend this class with your MyLinkedList to complete 
the implementation. 
*/ 
public abstract class AbstractLinkedList<E> { 
    protected final Node<E> myFront, 
    myBack; // dummy header/tail 
    protected int mySize; // # of elements in list 

    /* Constructs a new empty list. */ 
    public AbstractLinkedList() { 
     myFront = new Node<E> (null); 
     myBack = new Node<E> (null); 
     myBack.setPrevious(myFront); 
     myFront.setNext(myBack); 
     mySize = 0; 
    } 

    /* Inserts the given element at the given index. */ 
    public void add(int index, E element) { 
     checkIndex(index, size()); 

     Node<E> curr = getNodeAt(index); 

     // create the new node to hold the new element 
     Node<E> newNode = new Node<E> (element, curr.getPrevious(), curr); 

     (newNode.getNext()).setPrevious(newNode); 
     (newNode.getPrevious()).setNext(newNode); 

     mySize++; 
    } 

    /* Appends the given element to the end of this list. Returns true. */ 
    public void add(E element) { 
     add(size(), element); 
    } 

    /* 
    Removes the element of this list at the given index and returns it. 
    Throws IndexOutOfBoundsException if index is out of range. 
    */ 
    public void remove(int index) { 
     checkIndex(index, size() - 1); 

     // get the node to remove, and update the references 
     Node<E> nodeToRemove = getNodeAt(index); 

     (nodeToRemove.getPrevious()).setNext(nodeToRemove.getNext()); 
     (nodeToRemove.getNext()).setPrevious(nodeToRemove.getPrevious()); 

     mySize--; 
    } 

    /* 
    Sets the element of this list at the given index to have the given value. 
    Throws IndexOutOfBoundsException if index is out of range. 
    */ 
    public void set(int index, E value) { 
     checkIndex(index, size() - 1); 
     getNodeAt(index).element = value; 
    } 

    /* Returns the number of elements in this list. */ 
    public int size() { 
     return mySize; 
    } 

    /* Returns true if this list contains no elements. */ 
    public boolean isEmpty() { 
     return mySize == 0; 
    } 

    /* Removes all elements from this list. */ 
    public void clear() { 
     myFront.setNext(myBack); 
     myBack.setPrevious(myFront); 
     mySize = 0; 
    } 

    /* 
    Helper method: Throws an IndexOutOfBoundsException 
    if 0 <= index <= max is not true. 
    */ 
    private void checkIndex(int index, int max) throws IndexOutOfBoundsException { 
     if (index < 0 || index > max) throw new IndexOutOfBoundsException(); 
    } 

    /* 
    Removes the given element from this list, if it is present in the list. 
    Returns true if the element was in the list and was removed. 
    */ 
    public abstract boolean remove(E element); 

    /* Returns true if this list contains the given element. */ 
    public abstract boolean contains(E element); 
    /* 
    Returns the element of this list at the given index. 
    Throws IndexOutOfBoundsException if index is out of range. 
    */ 
    public abstract E get(int index); 

    /* 
    Returns the first index where the given element occurs in this list, 
    or -1 if the element is not in this list. 
    */ 
    public abstract int indexOf(E element); 

    /* 
    Returns the last index where the given element occurs in this list, 
    or -1 if the element is not in this list. 
    */ 
    public abstract int lastIndexOf(E element); 

    /* 
    Helper method: returns the node at the given index. 
    -1 returns dummy header, and size() returns the dummy tail. 
    Consider the effiency of this method. How can you write it 
    minimize the number of comparisons? 
    */ 
    protected abstract Node <E> getNodeAt(int index) throws IndexOutOfBoundsException; 

    /* 
    Returns an array containing all of the elements in this list 
    in the correct order. 
    */ 
    public abstract E[] toArray(); 

    /* 
    Returns a String representation of this list. 
    */ 
    public abstract String toString(); 

    /* Represents one doubly-linked node in the list. */ 
    protected class Node<E> { 
     private E element; 
     /* The data element */ 
     private Node <E> next; 

     /* Reference to the next node in the list */ 
     private Node<E> prev; 
     /* Reference to the previous node in the list */ 

     /* Constructs a new node to store the given element, with no next node. */ 
     public Node(E element) { 
      this(element, null, null); 
     } 

     /* Constructs a new node to store the given element and the given next node. */ 
     public Node(E element, Node<E> prev, Node<E> next) { 
      this.element = element; 
      this.prev = prev; 
      this.next = next; 
     } 
     /* Accessor methods. */ 
     public E getElement() { 
      return element; 
     } 

     public Node<E> getNext() { 
      return next; 
     } 

     public Node<E> getPrevious() { 
      return prev; 
     } 

     /* Mutator methods.*/ 
     public void setElement(E el) { 
      element = el; 
     } 

     public void setNext(Node<E> newNext) { 
      next = newNext; 
     } 

     public void setPrevious(Node<E> newPrev) { 
      prev = newPrev; 
     } 

     /* Returns a string representation of this node. */ 
     public String toString() { 
      return "(" + element + ")"; 
     } 
    } 

} 

我写的文件是MyLinkedList.java(下面找到)。

public class MyLinkedList<Object> extends AbstractLinkedList<Object>{ 

    private Node first, last; 
    private int mySize; 

     /** Create a default list */ 
     public MyLinkedList() { 
     } 


     /** Inserts the given element at the given index. */ 
     public void add(int index, Object element) { 
     checkIndex(index, size()); 

     Node curr = getNodeAt(index); 

     // create the new node to hold the new element 
     Node newNode = new Node(element, curr.getPrevious(), curr); 

     (newNode.getNext()).setPrevious(newNode); 
     (newNode.getPrevious()).setNext(newNode); 

     mySize++; 
     } 

     /** Removes all elements from this list. */ 
     public void clear() { 
     myFront.setNext(myBack); 
     myBack.setPrevious(myFront); 
     mySize = 0; 
     } 

    /** Appends the given element to the end of this list. Returns true. */ 
    public void add(Object element) { 
     add(size(), element); 
    } 

    /** 
    Removes the element of this list at the given index and returns it. 
    Throws IndexOutOfBoundsException if index is out of range. 
    */ 
    public void remove(int index) { 
     checkIndex(index, size() - 1); 

     //get the node to remove, and update the references 
     Node nodeToRemove = getNodeAt(index); 

     (nodeToRemove.getPrevious()).setNext(nodeToRemove.getNext()); 
     (nodeToRemove.getNext()).setPrevious(nodeToRemove.getPrevious()); 

     mySize--; 
    } 

    /* 
    Sets the element of this list at the given index to have the given value. 
    Throws IndexOutOfBoundsException if index is out of range. 
    */ 
    public void set(int index, Object value) { 
     checkIndex(index, size() - 1); 
     getNodeAt(index).element = value; 
    } 


     /** Returns the number of elements in this list. */ 
     public int size() { 
      return mySize; 
     } 

     /** Returns true if this list contains no elements. */ 
     public boolean isEmpty() { 
      return mySize == 0; 
     } 

     /** 
     Helper method: Throws an IndexOutOfBoundsException 
     if 0 <= index <= max is not true. 
     */ 
     private void checkIndex(int index, int max) throws IndexOutOfBoundsException{ 
     if (index < 0 || index > max) 
      throw new IndexOutOfBoundsException(); 
     } 

     /** Override toString() to return elements in the list */ 
     public String toString() { 
     StringBuffer result = new StringBuffer("["); 

     Node current = first; 
     for (int i = 0; i < mySize; i++) { 
      result.append(current.element); 
      current = current.next; 
      if (current != null) 
      result.append(", "); // Separate two elements with a comma 
      else 
      result.append("]"); // Insert the closing ] in the string 
     } 

     return result.toString(); 
     } 

     /** 
     Helper method: returns the node at the given index. 
     -1 returns dummy header, and size() returns the dummy tail. 
     Consider the effiency of this method. How can you write it 
     minimize the number of comparisons? 
     */ 
     @Override 
     protected MyLinkedList<Object>.Node getNodeAt(int index) throws IndexOutOfBoundsException{ 
      if (index == -1){ 
       return first; 
      } 
      else if (index == mySize){ 
        return last; 
      } 
     } 

     /** Return true if this list contains the element o */ 
     public boolean contains(Object o) { 
     // Implementation left as an exercise 
     return true; 
     } 

     /** Return the element from this list at the specified index */ 
     public Object get(int index) { 
      // Implementation left as an exercise 
      return null; 
     } 

     /** Returns the index of the first matching element in this list. 
     * Returns -1 if no match. */ 
     public int indexOf(Object o) { 
      // Implementation left as an exercise 
      return 0; 
     } 

     /** Returns the index of the last matching element in this list 
     * Returns -1 if no match. */ 
     public int lastIndexOf(Object o) { 
      // Implementation left as an exercise 
      return 0; 
     } 

     private class Node { 
     Object element; 
     Node next; 
     Node prev; 

     /** Constructs a new node to store the given element and the given next node. */ 
     public Node(Object element, Node prev, Node next) { 
      this.element = element; 
      this.prev = prev; 
      this.next = next; 
     } 

     /** Accessor methods. */ 
     public Object getElement(){ 
      return element; 
     } 

     public Node getNext(){ 
      return next; 
     } 

     public Node getPrevious(){ 
      return prev; 
     } 

     /** Mutator methods.*/ 
     public void setElement(Object obj){ 
      element = obj; 
     } 

     public void setNext(Node newNext){ 
      next = newNext; 
     } 

     public void setPrevious(Node newPrev){ 
      prev = newPrev; 
     } 

     /** Returns a string representation of this node. */ 
     public String toString() { 
      return "(" + element + ")"; 
     } 

     } 

    @Override 
    public boolean remove(Object element) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public Object[] toArray() { 
     return null; 

    } 
} 

,我不断收到错误是下面的行

@Override 
protected MyLinkedList<Object>.Node getNodeAt(int index) throws IndexOutOfBoundsException{ 
    if (index == -1){ 
     return first; 
    } 
    else if (index == mySize){ 
      return last; 
      } 
} 

Eclipse的不断告诉我说:“返回类型是AbstractLinkedList.getNodeAt(INT)不兼容”,我无法克服这个障碍。

任何和所有的帮助将不胜感激。

回答

1

您在这两个类里有一个嵌套Node类。

MyLinkedList中的方法声明为返回MyLinkedList.Node,但AbstractLinkedList要求返回类型AbstractLinkedList.Node

+0

好的,我将如何去解决这个问题? –

+0

两个节点类看起来都一样。在两种情况下删除一个并使用另一个。 – Jeremy

0

尝试改变

Node curr = getNodeAt(index); 

Node<Object> curr = getNodeAt(index);