将用空格分隔的整数读入int []数组 [英] Read integers separated with whitespace into int[] array

查看:104
本文介绍了将用空格分隔的整数读入int []数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();

示例输入

1 4 6 32 5

读取输入的最快方法是什么?把它放到一个整数数组 int []

What is the fastest way to read the input and put it into an integer array int[] ?

我也在寻找一些单行解决方案可能。

I'm also looking for some one-line solution if possible.

推荐答案

您可以使用扫描仪

Scanner scanner = new Scanner(System.in);
List<Integer> list = new ArrayList<Integer>();
while (scanner.hasNextInt())
  list.add(scanner.nextInt());
int[] arr = list.toArray(new int[0]);

在我们有java的闭包之前,这可能是你得到的最短的。

Until we have closures in java, this is probably the shortest you can get.

这篇关于将用空格分隔的整数读入int []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆