gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:35:06 25 4
gpt4 key购买 nike

我正在学习 Java,并且正在尝试单链表。我也在实现一个我为它创建的接口(interface),但我不断收到一个错误,说我的主类 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

链表接口(interface).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

链表.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

最佳答案

您正在实现原始接口(interface) LinkedListInterface , 其中E成为对象。通过添加 <E> 来实现通用版本:

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

关于java - 无法修复抽象方法错误 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19801982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com