文字列のリンクリストを作成するために、一般的なリンクリストを設計していました。
ただし、このエラーが発生し続けます:
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 クラスの定義を含めます。
LinkedList 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;
}
Node.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();
}
}
}
LinkedList.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;
}
}
Trial.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);
}