在标准输入的一行中,我有 3 种类型的整数:第一个整数是 id,第二个整数是 N - 某个数字,之后是 N 个整数,用一个空格分隔,我想将其存储在数组或 ArrayList 中。我如何使用 BufferedReader 来做到这一点?我有以下代码:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] line = br.readLine().split(" ");
int ID = Integer.parseInt(line[0]);
int N = Integer.parseInt(line[1]);
我的问题是有什么优雅的方法来读取该行的其余部分并将其存储到数组中吗?
Use Scanner
和方法hasNextInt()
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
arr[i]=scanner.nextInt();
i++;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)