正在处理java中的链接列表。当我在其中添加数据并打印出链接结果时,我得到的结果显示链表是空的,但我刚刚添加了一些数据,下面是我的代码。
import java.util.LinkedList;
public class workPls {
public LinkedList List = new LinkedList();
public void addNode(PlsWork st){
List.add(st);
}
public LinkedList getList(){
return List;
}
public void print(){
System.out.println(List.size());
System.out.println(List);
}
}
package linked;
import java.net.InetAddress;
public class Linked {
public static void main(String[] args) {
try {
InetAddress inetAddress = InetAddress.getLocalHost();
PlsWork ok = new PlsWork(inetAddress,"pc");
workPls oks = new workPls();
workPls okss = new workPls();
oks.addNode(ok);
oks.addNode(ok);
oks.addNode(ok);
okss.print();
}
catch (Exception error){
error.printStackTrace();
}
}
}
答案 0 :(得分:1)
您正在向oks添加数据并在okss上调用print,因此您将获得一个空列表。