for循环,多个输入没有停止

时间:2016-08-06 06:06:05

标签: java

我遇到了一个似乎没有停止的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;
 }

1 个答案:

答案 0 :(得分:1)

当你做的时候

size2 = listToBuild.sizeOfList();

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

我想,你没有改变firstNode的值。此外,我在你的功能中做了一些改变。

public int sizeOfList() {

  Node zNode = firstNode;
  sizeOfList = 0;

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