我正在尝试创建一个方法,将一个节点添加到链表,但到目前为止一直没有成功.这是我的代码与我的成员vars:
private Object data;
private int size = 0;
private Node head = null;
private Node tail = null;
public void add(Object item){
Node temp = head;
if (head != null) {
// THIS IS THE PROBLEM SITE
while(temp.getNext()!=null){
temp=temp.getNext();
}
//set next value equal to item
Node ab = (Node) item; // It says this is an invalid cast. How do I get around this??
ab.setNext(ab);
}
else{
head = new Node(item);
}
size++;
}
Run Code Online (Sandbox Code Playgroud)
这里也是我的Node类供参考:
public class Node …Run Code Online (Sandbox Code Playgroud)