我的編譯器警告我一個實例變量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();
}
向我們展示您的代碼! – MrSmith42
嗨,添加了代碼 – edd91
它是一個實例變量,有一個私有的final int [] []板;在班級的頂部。 – edd91