我正在设计一个通用链表来创建字符串的链表。
但是我不断收到此错误:
Exception in thread "main" java.lang.NoSuchMethodError: Node.<init>(Ljava/lang/Object;)V
at LinkedList.addNode(LinkedList.java:10)
at LinkedList.<init>(LinkedList.java:22)
at Trial.main(Trial.java:7)
从堆栈跟踪中,错误是在 LinkedList 的 addNode() 方法中生成的。我包括此方法的定义以及 Node 类的定义。
链表 addNode()
public void addNode(T n) {
Node<T> temp = new Node<T>(n);
if(start==null) {
start = temp;
current = start;
} else {
end.setNext(temp);
}
end =temp;
}
节点.java
public class Node<T>{
private T n;
Node next;
Node(T n) {
this.n = n;
next = null;
}
public void setNext(Node nextNode) {
next = nextNode;
}
public Node getNext() {
return next;
}
public T getN() {
return n;
}
@Override
public String toString() {
if(n instanceof String)
return n.toString();
else {
return T.toString();
}
}
}
链表.java
public class LinkedList<T>{
Node start;
Node end;
Node current;
private static final long serialVersionUID = 901L;
LinkedList(T n) {
addNode(n);
}
public void addNode(T n) {
Node<T> temp = new Node<>(n);
if(start==null) {
start = temp;
current = start;
} else {
end.setNext(temp);
}
end =temp;
}
LinkedList(T[] n) {
for(T print : n)
addNode(print);
}
public void addNode(T[] n) {
if(n!=null) {
for (T values : n) {
addNode(values);
}
}
}
public void incC() {
current = current.getNext();
}
public void insert(T n) {
Node newNode = new Node(n);
if(current==start){
newNode.setNext(current);
start = newNode;
}else {
Node tempstart = start;
Node prevAdd=null;
while(tempstart!=current){
prevAdd = tempstart;
tempstart = tempstart.getNext();
}
prevAdd.setNext(newNode);
newNode.setNext(current);
}
}
public void find(T x) {
Node tempstart;
tempstart = start;
while (tempstart!=null) {
if(tempstart.getN()==x) {
System.out.println("Element found");
tempstart = tempstart.getNext();
} else {
tempstart = tempstart.getNext();
}
}
}
public void delete(T x) {
Node previous=null;
Node tempstart = start;
while(tempstart!=null) {
if(tempstart.getN()==x) {
if(previous ==null) {
previous = tempstart;
tempstart = tempstart.getNext();
start = tempstart;
previous.setNext(null);
previous = null;
} else {
tempstart = tempstart.getNext();
previous.setNext(tempstart);
}
}else {
previous = tempstart;
tempstart = tempstart.getNext();
}
}
}
@Override
public String toString() {
Node tempNode = start;
String str = "Values: ";
while (tempNode!=null) {
str = str + " " + tempNode.toString();
tempNode = tempNode.getNext();
}
return str;
}
}
试用版.java
public class Trial {
public static void main(String[] args) {
String[] para = {"Hollo","this","is","me"};
LinkedList<String> L1;
L1 = new LinkedList<String>(para);
System.out.println(L1);
}