我的编译器警告我一个实例变量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