如果要读取:
13 1 2 3 4 5 6 7 8 9 10 20 16 18这样的数据,甚至更多,用scanner时间很长,测评系统只有100ms时间,所以用bufferedreader读取,请问怎么实现一个个读取,并把他们放入数组。
这是我用scanner实现的:
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int []num = new int[N];
for (int i = 0; i < N; i++)
{
num[i] = in.nextInt();
}
感谢!
可以这样:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BufferedReaderTest
{
public static void main(String[] args) throws IOException {
BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
int num[] = new int[1000];
String[] strNums;
strNums = bi.readLine().split("\\s");
for(int i=0; i<strNums.length; i++) {
num[i] = Integer.parseInt(strNums[i]);
}
}
}
基本思路是整行读入,然后用split函数分割处理后存入数组。
注意输入格式应如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
如果对您有帮助,请采纳答案好吗(鼠标在我的答案区域停留片刻即可看到采纳按钮),谢谢!