2017-04-07 73 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