具有多个输入的for循环不停止

好奇的

我遇到了似乎没有停止的for循环问题。当我第一次在自定义列表中仅使用一个项目调用outPutGenerator时,它运行良好。当我用9个项目再次运行它时,它一直在运行。您能指出我正确的方向吗?

编辑:我可以让测试第二次运行,但是此后什么都没有。

input: 
 1
 5
 3
 3 2 4
 1 5 2
 3 6 4
 1
 6
 2
 5 6
 6 7

代码:

 public static void outputGenerator (CustomList [] list, PrintWriter  printWriter) {
  int size2;
  int valueToPrint;
  CustomList listToBuild;
  int length;
  length = list.length;
  System.out.println("len: " + length);

  //print out the results to a .txt file
  try {

     printWriter.println("Matrix read: ");
     printWriter.println();
     printWriter.println("------------------" +
           "---------------------");
     printWriter.println();
     printWriter.flush();

     for (int x = 0; x < length; x++){
        System.out.println("test");
        listToBuild = list[x];
        size2 = listToBuild.sizeOfList();
        System.out.println("size2 " + size2);

        for (int y = 0; y < size2; y++) {
           System.out.println("y: " + y);
           valueToPrint = listToBuild.ValueOfNode(y);
           printWriter.println(valueToPrint);
           System.out.println("val" + valueToPrint);
           printWriter.flush();

        }
        printWriter.println();
     }
     return;
  }catch (Exception e) {
     e.printStackTrace();
  }
}

自定义链接列表代码:

public class CustomList {

private Node firstNode;
private Node end;
private Node header;
private int sizeOfMatrix;
private int sizeOfList;




//constructor to set all to blank
public CustomList() {
  firstNode = null;
  end = null;
  header = null;
  sizeOfMatrix = 0;
  sizeOfList = 0;

}

public void addToList(int dataToSave) {

  Node node = new Node (dataToSave);

  if (firstNode == null) {
     firstNode = node;
     firstNode.next = end;
     firstNode.before = header;
  }

  else if (end == null) { 
     end = node;
     end.before = firstNode;
     firstNode.next = end;

  }

  else 
     end.next = node;
  node.before = end;
  end = node;

  sizeOfMatrix++;
}

public void setHeader (int dataToUse){

  Node headerNode = new Node(dataToUse);
  header = headerNode;
  header.next = firstNode;
}

public void print() {
  Node zNode = firstNode;
  System.out.println("Test");
  if(firstNode == null){
     System.out.print("EMPTY");
     return;
  }

  while (zNode != null) {
     System.out.println(zNode);
     zNode = zNode.next;
  }
}




public int sizeOfList() {

  Node zNode = firstNode;
  sizeOfList = 0;

  while( zNode != null) {

     zNode = firstNode.next;
     sizeOfList++;
  }
  return sizeOfList; 
}



 public int ValueOfNode( int column ) {
  int counter = 0;
  Node zNode = firstNode;

  while (zNode != null) {

     if (column == counter){
        return zNode.numInMatrix();
     }

     else
        zNode = firstNode.next;
        counter++;
  }

  return -1;
 }
秋季学习

当你做的时候

size2 = listToBuild.sizeOfList();

sizeOfList()将始终为每个调用返回相同的值。

我想,您不会更改firstNode的值。而且,我对您的功能进行了一些更改。

public int sizeOfList() {

  Node zNode = firstNode;
  sizeOfList = 0;

  while( zNode != null) {
     zNode = zNode.next;
     sizeOfList++;
  }
  return sizeOfList; 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章