无法修复抽象方法错误(Java)

时间:2013-11-06 00:46:08

标签: java generics implementation abstract singly-linked-list

我正在学习Java,我正在尝试单个链表。我正在实现我为它制作的接口,但是我一直收到一个错误,说我的主类LinkedList“不是抽象的,因此不会覆盖LinkedListInterface中的方法搜索(java.lang.Object)”。我究竟做错了什么?我似乎正在实现我的所有方法,但我仍然遇到错误。

这是我的源代码:Node.java

public class Node<E> {

private E element;
private Node<E> next;

public Node() {
    this(null, null);
}

public Node(E element, Node<E> next) {
    this.element = element;
    this.next = next;
}

public E getElement() {
    return element;
}

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

public void setElement(E element) {
    this.element = element;
}

public void setNext(Node<E> next) {
    this.next = next;
}
} // End Node

LinkedListInterface.java

public interface LinkedListInterface<E> {

/**
*  Adds the specified element to the head of the linked list.
*  @param element element to be added to the head of the list.
*/
public void addFirst(E element);

/**
*  Removes and return the element from the head of the linked list.
*  @return the element removed from the head of the linked list.
*/
public E removeFirst();

/**
*  Adds the specified element to the end of the linked list.
*  @param element element to be added to the end of the list.
*/
public void addLast(E element);

/**
*  Removes and return the element from the end of the linked list.
*  @return the element removed from the end of the linked list.
*/
public E removeLast(); //EXTRA CREDIT

/**
*  Returns a string representation of the linked list.
*  @return a string representation of the linked list.
*/
public String traverse();

/**
*  Returns a boolean of true if the searched term is within the list.
*  @return a boolean.
*/
public boolean search(E element);

} // End LinkedListInterface 

LinkedList.java:

public class LinkedList<E> extends Node<E> implements LinkedListInterface {

//variables
private Node<E> head;
private Node<E> tail;
private int size;

//Constructor
public LinkedList() {
  head = null;
    tail = null;
  size = 0;
}

// Check that list is empty
public boolean isEmpty() { 
  if (head == null)
     return true;
  else
     return false;
}

// Return the size of the list
public int size() { 
  return size;
}

// Add element to the first position on the list
public void addFirst(E element) { 
  Node<E> temp = new Node<E>(element, null);
  if (isEmpty()) {
     head = temp; //tail should also be updated...
  } else {
     temp.setNext(head); //points temps next to head
     head = temp; //move head to temp
  }
  size++;

}// End addFirst

// Remove element at the first position on the list
public E removeFirst() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = head; //point to the head
  E result = head.getElement(); 
  head = head.getNext(); //set the new head to the next element
  temp.setNext(null); //set temp next to null which removes it
  size--; //decrease list size

  return result;

}// End removeFirst

// Add element to the last position on the list
public void addLast(E element) { 
  Node<E> temp = new Node<E>(element, null);
  if (isEmpty())
     tail = temp; //tail should also be updated...
  else {
     temp.setNext(tail); //points temps next to head
     tail = temp; //move head to temp
  }
  size++;

}// End addLast

// Remove element at the last position on the list
public E removeLast() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = tail; //point to the head
  E result = tail.getElement(); //
  tail = tail.getNext(); //set the new head to the next element
  temp.setNext(null); //set temp next to null which removes it
  size--; //decrease list size

  return result;
}// End removeLast

// Returns a string representation of the list
public String traverse() {
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> temp = head;

  String result = "Head-->";
  int i = size();
  while (i > 0){
     result += temp.getElement() + "-->";
     temp = temp.getNext();
     i--;
  }  
  return result;
}// End traverse

// Returns true if searched item exists within the list
public boolean search(E element) { 
  if (isEmpty())
     throw new EmptyListException("error");

  Node<E> itr = head.getNext();

  while (!itr.getElement().equals(element)) {
     itr = itr.getNext();
  }
    return true;
}// End search

public static void main(String [] args) {

  LinkedList<String> myList = new LinkedList<String>();

  myList.addFirst("MSP");
  myList.addFirst("RST");
  myList.addFirst("STL");

  System.out.println(myList.traverse());

  myList.removeFirst();

  System.out.println(myList.traverse());

}// End main

}// End LinkedList class

1 个答案:

答案 0 :(得分:3)

您正在实现原始接口LinkedListInterface,其中E成为对象。通过添加<E>

来实现通用版本
//                                                                       vvv
public class LinkedList<E> extends Node<E> implements LinkedListInterface<E> {