Java 运行链表后没有显示输出
我有三个java类,它们构造节点并实现几种方法来操作链表。我的节点类包含一个节点。“我的节点列表”类包含操作链接列表的方法。我的link方法构建一个链表,我的print方法是打印链表。我使用冒泡排序方法对链表进行排序。当我从类列表中的main方法调用这3个方法时,控制台中没有显示任何输出。我已经尝试了所有的方法,但是我还没有打印出一些输出,并且没有错误消息表明我的代码有问题 节点类Java 运行链表后没有显示输出,java,linked-list,output,Java,Linked List,Output,我有三个java类,它们构造节点并实现几种方法来操作链表。我的节点类包含一个节点。“我的节点列表”类包含操作链接列表的方法。我的link方法构建一个链表,我的print方法是打印链表。我使用冒泡排序方法对链表进行排序。当我从类列表中的main方法调用这3个方法时,控制台中没有显示任何输出。我已经尝试了所有的方法,但是我还没有打印出一些输出,并且没有错误消息表明我的代码有问题 节点类 public class iNode{ public int item; public iNode next;
public class iNode{
public int item;
public iNode next;
public iNode(int i, iNode n){
item = i;
next = n;
}
public iNode(int i){
item = i;
next = null;
}
// Node class
public int getItem() {
return this.item;
}
节点列表类
public class iNode_List {
public static iNode head;
public static int size;
public iNode_List(){
this.head = null;
this.size = 0;
}
public static iNode link(int n, int m){
iNode previous;
iNode current;
int i = 0;
previous = null;
while (i < n) {
current = new iNode(ThreadLocalRandom.current().nextInt(0, m-1), previous);
previous = current;
head = current;
i++;
}
return previous;
}
public static void print() {
iNode currentNode = head;
while(currentNode != null) {
int data = currentNode.getItem();
System.out.println(data);
currentNode = currentNode.next;
}
}
public static void Bubble_Sort (){
if (size > 1) {
for (int i = 0; i < size; i++ ) {
iNode currentNode = head;
iNode next = head.next;
for (int j = 0; j < size - 1; j++) {
if (currentNode.item > next.item) {
int temp = currentNode.item;
currentNode.item = next.item;
next.item = temp;
}
currentNode = next;
next = next.next;
}
}
}
}
您的问题是,您从未将任何值赋给
头
,但您的方法会打印头
,该值始终为空。这就是为什么什么都没出现
您可能需要添加
head=current
靠近链接
方法的末尾。在x.list
中的列表
是什么?很抱歉,我将方法的名称从一个列表更改为另一个链接,这样人们就不会对类名产生混淆。我来编辑一下。你把LinkedList弄得太复杂了。我不知道为什么。你会发现很多例子。首先,您需要了解如何创建LinkedList数据结构,然后打印它们。然后,尝试其他的事情。从学校的援助哈哈。教授们想让一切变得更复杂。我决不会让头部
和大小
变量静态
。谢谢你的帮助。但我仍然无法使冒泡排序工作,因为它没有对元素进行排序。但是输出会打印随机整数。Alan,现在可能是学习使用调试器的好时机。这样,您就可以一步一步地浏览代码,并在每一步之后查看每个变量的值。我相信你会很快找到问题的。如果没有,您可以问一个单独的堆栈溢出问题。
public class list {
public static void main (String [] args){
iNode_List x = new iNode_List();
x.link(10, 10);
x.Bubble_Sort();
x.print();
}