2017-03-05 3 views
1

Ich bin so kurz davor, mein Projekt abzuschließen, aber ich bin auf einem Teil fest. Ich versuche abstrakte Methoden aus einer Datei namens AbstractLinkedList.java in meiner Datei MyLinkedList.java zu implementieren.MyLinkedList.java Projekt Ausgabe

Hier sind die beiden Projektdateien:

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 + ")"; 
     } 
    } 

} 

Die Datei, die ich geschrieben habe, ist MyLinkedList.java (unten zu finden).

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; 

    } 
} 

Der Fehler, dass ich immer halten, ist mit der folgenden Zeile

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

Eklipse hält mir zu sagen, dass „Der Rückgabetyp mit AbstractLinkedList.getNodeAt (int) unvereinbar ist“, und ich bin nicht in der Lage zu überwinden dieses Hindernis.

Jede und alle Hilfe würde sehr geschätzt werden.

Antwort

1

Sie eine verschachtelte Node Klasse in beiden Klassen haben.

Das Verfahren in MyLinkedList erklärt MyLinkedList.Node zurückzukehren, aber AbstractLinkedList erfordert den Rückgabetyp von AbstractLinkedList.Node.

+0

Ok, wie würde ich dieses Problem lösen? –

+0

Beide Knoten-Klassen sehen identisch aus. Entfernen Sie einen und verwenden Sie den anderen in beiden Szenarien. – Jeremy

0

Try Ändern

Node curr = getNodeAt(index); 

zu

Node<Object> curr = getNodeAt(index);