2017-04-07 76 views
0

我的编译器警告我一个实例变量2d int [] []数组,可能在我分配它时未初始化。编译器认为最终的实例变量未初始化

我明白为什么编译器可能会这么想,因为它是在double if语句中初始化的。然而,第一个if是在初始化为true的布尔值上,第二个if在else上抛出异常。我对程序的逻辑有信心,但编译器显然不是。

有没有人有任何提示克服这种问题?我不想以其他方式初始化变量,因为它意味着最终的结果。

关注的变量是董事会变量。下面是包含变量的对象的构造函数的一部分。

try { 
     FileReader fr = new FileReader(filename); 
     BufferedReader br = new BufferedReader(fr); 
     boolean first = true; 
     int lineCount = 0; 
     String line; 

     while ((line = br.readLine()) != null) { 
      String lineParts[] = line.split(" "); 
      if (first) { 
       if (lineParts.length == 2) { 
        this.xSize = Integer.parseInt(lineParts[0]); 
        this.ySize = Integer.parseInt(lineParts[1]); 
        board = new int[this.ySize][this.xSize]; 
        first = false; 
       } else { throw new RuntimeException(); } 
      } else { 
       lineCount++; 
       if (lineParts.length == this.xSize) { 
        for (int i = 0; i < this.xSize; i++) { 
         board[lineCount][i] = Integer.parseInt(lineParts[i]); 
        } 
       } else throw new RuntimeException(); 
      } 
     } 
     br.close(); 
     if (lineCount != this.ySize) throw new RuntimeException(); 


    } 
+2

向我们展示您的代码! – MrSmith42

+0

嗨,添加了代码 – edd91

+0

它是一个实例变量,有一个私有的final int [] []板;在班级的顶部。 – edd91

回答

1

事实上,编译器无法解开循环逻辑,知道final变量在使用前已经初始化。

你需要移动第一线的处理圈外  —这是合理的,无论如何,因为循环的内容是第一行和后续行几乎完全不同:

try { 
    FileReader fr = new FileReader(filename); 
    BufferedReader br = new BufferedReader(fr); 
    int lineCount = 0; 
    String line; 

    line = br.readLine(); 
    if (line != null) { 
     String lineParts[] = line.split(" "); 
     if (lineParts.length == 2) { 
      this.xSize = Integer.parseInt(lineParts[0]); 
      this.ySize = Integer.parseInt(lineParts[1]); 
      board = new int[this.ySize][this.xSize]; 
     } else { 
      throw new RuntimeException(); 
     } 
     while ((line = br.readLine()) != null) { 
      String lineParts[] = line.split(" "); 
      lineCount++; 
      if (lineParts.length == this.xSize) { 
       for (int i = 0; i < this.xSize; i++) { 
        board[lineCount][i] = Integer.parseInt(lineParts[i]); 
       } 
      } else { 
       throw new RuntimeException(); 
      } 
     } 
    } 
    br.close(); 
    if (lineCount != this.ySize) throw new RuntimeException(); 
} 

注意:此代码保留了先前代码的行为,它不计算第一行。我猜这是特别的事实,不包括在内。 :-)


附注:我会强烈建议在该代码使用try-与资源,不仅为最佳实践,而是因为你没有关闭文件,当你把你的异常:

try (
    FileReader fr = new FileReader(filename); 
    BufferedReader br = new BufferedReader(fr); 
) { 
    int lineCount = 0; 
    String line; 

    line = br.readLine(); 
    if (line != null) { 
     String lineParts[] = line.split(" "); 
     if (lineParts.length == 2) { 
      this.xSize = Integer.parseInt(lineParts[0]); 
      this.ySize = Integer.parseInt(lineParts[1]); 
      board = new int[this.ySize][this.xSize]; 
     } else { 
      throw new RuntimeException(); 
     } 
     while ((line = br.readLine()) != null) { 
      String lineParts[] = line.split(" "); 
      lineCount++; 
      if (lineParts.length == this.xSize) { 
       for (int i = 0; i < this.xSize; i++) { 
        board[lineCount][i] = Integer.parseInt(lineParts[i]); 
       } 
      } else { 
       throw new RuntimeException(); 
      } 
     } 
    } 
    if (lineCount != this.ySize) throw new RuntimeException(); 
} 
+0

谢谢TJ Crowder!还了解到不在你的循环中混合内容。你说得对,第一行不算,它只包含第二块电路板的尺寸信息。 – edd91

+0

@ edd91:很高兴帮助。我刚才添加了关于试用资源的第二部分。 –

+0

谢谢! – edd91