What's the fastest way to read from System.in in Java?

前端 未结 9 2105
梦如初夏
梦如初夏 2020-11-30 19:17

I am reading bunch of integers separated by space or newlines from the standard in using Scanner(System.in).

Is there any faster way of doing this in Ja

9条回答
  •  时光说笑
    2020-11-30 19:51

    StringTokenizer is a much faster way of reading string input separated by tokens.

    Check below example to read a string of integers separated by space and store in arraylist,

    String str = input.readLine(); //read string of integers using BufferedReader e.g. "1 2 3 4"
    List list = new ArrayList<>();
    StringTokenizer st = new StringTokenizer(str, " ");
    while (st.hasMoreTokens()) {
        list.add(Integer.parseInt(st.nextToken()));
    } 
    

提交回复
热议问题