Java 链表,不知道我哪里出错了,
我的主类位于最底部,我正在尝试将节点添加到列表中,并在主类中像spcfied一样显示它们Java 链表,不知道我哪里出错了,,java,Java,我的主类位于最底部,我正在尝试将节点添加到列表中,并在主类中像spcfied一样显示它们 lList - print linkedlist: lList.size() - print linkedlist size: 1 lList.size() - print linkedlist size: 1 lList - print linkedlist: 我的单子不会显示,有人能指出哪里不对吗 public inter
lList - print linkedlist:
lList.size() - print linkedlist size: 1
lList.size() - print linkedlist size: 1
lList - print linkedlist:
我的单子不会显示,有人能指出哪里不对吗
public interface ListInterface {
//List operations
public boolean isEmpty();
public int size();
public void add(int index, Object item);
public void remove(int index);
public void removeAll();
}// end interface
首先,您的print调用节点上的toString,它只打印节点的地址,而不是您想要的。您需要有一个toString方法,该方法将返回节点中的值,以便正确显示列表 第二,你的设计可能是由老师指定的,但是指定按位置添加到列表中是愚蠢的。至少,提供addLast和addFirst方法将使您能够更轻松地完成您显然想做的事情,即构建列表 在本例中,我假设您希望addLast,因为您希望按顺序构建列表。 这将是非常低效的,因为您每次都必须扫描整个列表,以便在最后存放一个新元素,但可能这就是赋值
如果可以的话,从清理API开始。写入addLast,将元素添加到末尾。如果仍然有问题,请重新发布代码。您真的不应该跟踪位置以向末尾添加值。如果只有一个元素,则
head.getNext()
返回null
。您的添加方法有缺陷。只有第一个索引会修改列表…但我添加了5个元素,.add(0,“1”)@UmNyobe如何获得要添加的其余元素?只有当索引为0时,我才应该对add方法add
进行什么修改。否则它什么也不做。你想用第一个if
语句实现什么?不,问题不在于“太远了”。注意,当他调用add
时,他使用索引0、1、2、3、4调用它。所以我需要第二个add方法,只插入第一个节点?然后使用ADD i ALREADY创建来完成索引1,2,3,4中的元素,ajb是正确的。add函数本身有问题。你有一些奇怪的逻辑。你想做什么?如果index=0,则在前面添加,对吗?如果索引是其他内容,请添加到何处?
public class Node {
Object item;
Node next;
Node(Object newItem) {
item = newItem;
next = null;
}
Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
}
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
// end class Node
}
public class ListReferencedBased implements ListInterface {
private Node head;
private int numItems;
public ListReferencedBased(){
numItems = 0;
head = null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return numItems == 0;
}
public int size() {
// TODO Auto-generated method stub
return numItems;
}
private Node find(int index){
Node curr = head;
for(int skip = 0;skip < index;skip++){
curr = curr.next;
}
return curr;
}
public void add(int index, Object item) {
// TODO Auto-generated method stub
if(index == 0 && index < numItems + 1){
if(index ==0){
// insert in begning
Node newNode = new Node(item, head);
head = newNode;
}
else{
Node prev = find(index - 1);
Node newNode = new Node(item,prev.next);
prev.next = newNode;
}
numItems++;
}
}
@Override
public void remove(int index) {
// TODO Auto-generated method stub
if (index == 0){
head = head.next;
}
else{
Node prev = find(index -1);
Node curr = prev.next;
prev.next = curr.next;
}
numItems--;
}
@Override
public void removeAll() {
head = null;
numItems = 0;
}
public String toString() {
Node crunchifyCurrent = head.getNext();
String output = "";
while (crunchifyCurrent != null) {
output += "[" + crunchifyCurrent.toString() + "]";
crunchifyCurrent = crunchifyCurrent.getNext();
}
return output;
}
}
public class Nodemain {
public static void main(String[] args) {
ListReferencedBased lList = new ListReferencedBased();
// add elements to LinkedList
lList.add(0,"1");
lList.add(1,"2");
lList.add(2,"3");
lList.add(3,"4");
lList.add(4,"5");
/*
* Please note that primitive values can not be added into LinkedList
* directly. They must be converted to their corresponding wrapper
* class.
*/
System.out.println("lList - print linkedlist: " + lList);
System.out.println("lList.size() - print linkedlist size: " + lList.size());
// System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
// System.out.println("lList.remove(2) - remove 2nd element: " + lList.remove(2));
// System.out.println("lList.get(3) - get 3rd element: " + lList.get(3));
System.out.println("lList.size() - print linkedlist size: " + lList.size());
System.out.println("lList - print linkedlist: " + lList);
}
}