Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将始终为“null”的输出修复为1,2,3“null”?_Java - Fatal编程技术网

Java 如何将始终为“null”的输出修复为1,2,3“null”?

Java 如何将始终为“null”的输出修复为1,2,3“null”?,java,Java,我正在编写一个main方法来测试一些代码,但是我的main模式有一些错误,无法正确测试代码。有人能帮我用我的主要方法解决这个问题吗 以下是测试类的一些背景: 在DrJava中输入LLNode类: /** * The node of a linked list */ public class LLNode<T> { /** the element stored in the node */ private T element; /** a re

我正在编写一个main方法来测试一些代码,但是我的main模式有一些错误,无法正确测试代码。有人能帮我用我的主要方法解决这个问题吗

以下是测试类的一些背景:

在DrJava中输入LLNode类:

 /**
   * The node of a linked list
   */
   public class LLNode<T> {
   /** the element stored in the node */
   private T element;

    /** a reference to the next node of the list */
    private LLNode<T> next;

   /**
    * the constructor
    * @param element  the element to store in the node
    * @param next  a reference to the next node of the list
    */
     public LLNode(T element, LLNode<T> next) {
     this.element = element;
     this.next = next;
   }

   /**
    * Returns the element stored in the node
    * @return the element stored in the node
    */
  public T getElement() {
     return element;
  }

  /**
   * Returns the next node of the list
   * @return the next node of the list
   */
  public LLNode<T> getNext() {
    return next;
  }

  /**
   * Changes the node that comes after this node in the list
   * @param next  the node that should come after this node in the 
 list.  It can be null.
   */
  public void setNext(LLNode<T> next) {
    this.next = next;
  }

}
提示:您可以在交互窗格中使用3行代码来完成此操作。为了好玩,试着用一句话来做吧!您可以通过在交互窗格中键入listHead.getElement、listHead.getNext.getElement、listHead.getNext.getNext.getElement和listHead.getNext.getNext.getNext对其进行测试,如果列表正确,则输出应为1、2、3和null

在上图中,每个框都引用一个LLNode对象,框中的左侧项是元素,右侧项是下一个引用

第2步:尝试创建相同的链表,但现在使用一个循环,在每个迭代中,它创建一个LLNode实例。要创建这三个节点,循环将需要迭代三次。提示:向前或向后创建列表更容易吗

import java.util.*;
public class prelab10 {
     public static void main(String args[]){
          LLNode start = null;
          LLNode end = null;
          int i = 0;
        int size = 3;
      Scanner scan = new Scanner(System.in);

      LLNode list = new LLNode(1,null);
      LLNode list1 = new LLNode(2,null);
      LLNode list2 = new LLNode(3,null);

      start = list; 
      end = start;

      list.setNext(start);
           start = list;

      list1.setNext(start);
           start = list1;

      list2.setNext(start);
           start = list2;

      LLNode a =start ;

      while(i!= size)

      {
          i++;
          System.out.println(a.getElement()+"<->");
          a = a.getNext();
      }
      while(i == 3)
           System.out.println("null");

}     
}

我希望输出是1,2,3,从头到尾都是null而不是null

我建议您逐行检查您正在构建的结构

事实上,你已经非常接近了,你建造的结构是:

list3 -> list2 -> list1 -> list1
请注意,list1指向自身,因此如果删除list1.setNextstart行,则应获得:

list3 -> list2 -> list1 -> null
在这里你可以看到:

public class prelab10 {
  public static void main(String args[]){

    LLNode<Integer> start = null;
    LLNode<Integer> end = null;
    LLNode<Integer> list1 = new LLNode<Integer>(1, null);
    LLNode<Integer> list2 = new LLNode<Integer>(2, null);
    LLNode<Integer> list3 = new LLNode<Integer>(3, null);

    start = list1; 
    end = start;
    // start -> list1
    // end -> list1

    // This one is wrong, list1 should NOT point to itself:
    // list1.setNext(start);
    start = list1;
    // list1.next -> start -> list1
    // start -> list1

    list2.setNext(start);
    start = list2;
    // list2.next -> start -> list1
    // start -> list2

    list3.setNext(start);
    start = list3;
    // list3.next -> start -> list2
    // start -> list3

    LLNode cursor = start;
    // cursor -> start -> list3

    // So, what you have build is:
    // list3 -> list2 -> list1 -> list1

    // Removing list1.setNext(start), you would have:
    // list3 -> list2 -> list1 -> null

    // This loop will never end in your original code, as cursor will never be
    // null, as list1's next pointer points to itself:

    while (cursor != null) {
      System.out.print(cursor.getElement() + " -> ");
      cursor = cursor.getNext();
    }

  }     
}
这将帮助您调试和修复代码


您可能还希望以相反的顺序链接节点。

看起来很像提示告诉您从末尾开始,这是唯一一个没有下一个节点的节点。向后工作时,应该将先前创建的节点传递到构造函数:list1=new LLNode2,list2。问题还建议您使用循环。谢谢,我会仔细阅读并重新安排我的想法。
public class prelab10 {
  public static void main(String args[]){

    LLNode<Integer> start = null;
    LLNode<Integer> end = null;
    LLNode<Integer> list1 = new LLNode<Integer>(1, null);
    LLNode<Integer> list2 = new LLNode<Integer>(2, null);
    LLNode<Integer> list3 = new LLNode<Integer>(3, null);

    start = list1; 
    end = start;
    // start -> list1
    // end -> list1

    // This one is wrong, list1 should NOT point to itself:
    // list1.setNext(start);
    start = list1;
    // list1.next -> start -> list1
    // start -> list1

    list2.setNext(start);
    start = list2;
    // list2.next -> start -> list1
    // start -> list2

    list3.setNext(start);
    start = list3;
    // list3.next -> start -> list2
    // start -> list3

    LLNode cursor = start;
    // cursor -> start -> list3

    // So, what you have build is:
    // list3 -> list2 -> list1 -> list1

    // Removing list1.setNext(start), you would have:
    // list3 -> list2 -> list1 -> null

    // This loop will never end in your original code, as cursor will never be
    // null, as list1's next pointer points to itself:

    while (cursor != null) {
      System.out.print(cursor.getElement() + " -> ");
      cursor = cursor.getNext();
    }

  }     
}