【发布时间】:2020-11-25 01:44:11
【问题描述】:
我正在尝试使用扫描仪读取二维 3x3 矩阵的元素。 输入看起来像这样:
3
11 2 4
4 5 6
10 8 -12
我目前收到错误:
Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
scan.next();
List<List<Integer>> array = new ArrayList<>();
for (int i = 0; i < a; i++) {
String number = scan.nextLine();
String[] arrRowItems1 = number.split(" ");
List<Integer> list = new ArrayList<>();
for (int j = 0; j < a; j++) {
int arrItem = Integer.parseInt(arrRowItems1[j]);
list.add(arrItem);
}
array.add(list);
}
scan.close();
我该如何解决这个问题,以便最终根据用户输入构造一个 2d 3x3 矩阵数组?谢谢。
【问题讨论】:
-
一行的输入应该是
11 2 4,还是应该是单独的输入11、2和4? -
嗨,数组中的每个元素都是一行中的单独输入。
标签: java arrays list matrix integer