我有一个包含一些值的文件:
11
8
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 1 1 1 1 1 1 0 0
0 1 1 0 1 1 1 0 1 1 0
1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1 0 1
1 0 1 0 0 0 0 0 1 0 1
0 0 0 1 1 0 1 1 0 0 0
我需要将这些值读入 2D ArrayList
。前两个值(11 和 8)分别是行数和列数。所以这里是代码:
Scanner scanner = new Scanner(file);
int x, y;
x = scanner.nextInt();
System.out.println(x + " has been read");
y = scanner.nextInt();
System.out.println(y + " has been read");
ArrayList<ArrayList<Boolean>> pixelMap;
pixelMap = new ArrayList<ArrayList<Boolean>>();
ArrayList<Boolean> buffer_line = new ArrayList<Boolean>();
Boolean buffer;
for (int i = 0; i < x; i++){
for (int j = 0; j < y; j++){
buffer = scanner.nextBoolean();
System.out.println(buffer + " has been read");
//buffer_line.add(buffer);
}
//pixelMap.add(buffer_line);
//buffer_line.clear();
}
问题是 - 程序成功读取了前两个数字,当涉及到布尔值时,它会在线抛出 InputMismatch 异常
buffer = scanner.nextBoolean();
所以我无法理解为什么。0
接下来应该阅读它,它是布尔值 - 那么实际上不匹配的是什么?
我还指出,如果将buffer
type 更改为 integer 然后 assign scanner.nextInt()
,程序将正确读取所有值,因此在输出中我会看到所有这些值。所以当然,我可以更改ArrayList
为 Integer 来完成这项工作,但它在语义上是错误的,因为它只包含布尔值。谁能帮我找出问题所在?