java - 更有效地从控制台读取 int 值

标签 java input user-input

如何比这更有效地(从内存)从控制台读取 int 值:

BufferedReader in ...
number = Integer.parseInt(in.readLine());

当我使用readLine()并将其解析为int时,java创建许多String对象并消耗内存。我尝试使用 Scanner 和方法 nextInt() ,但这种方法也不是那么有效。

P.S I need read > 1000_000 values and I have memory limit.

编辑任务的完整代码

import java.io.*;

public class Duplicate {

    public static void main(String[] args) throws IOException {

        int last = 0;
        boolean b = false;

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

        int n = Integer.parseInt(reader.readLine());

        for (int i = 0; i < n; i++) {
            int number =Integer.parseInt(reader.readLine());
            if (number == 0 && !b) {
                System.out.println(0);
                b = true;
            }
            if (number == last) continue;
            last = number;
            System.out.print(last);
        }
    }
}

并重写变体:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

public class Duplicate {

    public static void main(String[] args) throws IOException {

        int last = 0;
        boolean b = false;

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int nextInt = getNextInt(reader);

        for (int i = 0; i < nextInt; i++) {
            int number = getNextInt(reader);
            if (number == 0 && !b) {
                System.out.println(0);
                b = true;
            }
            if (number == last) continue;
            b = true;
            last = number;
            System.out.println(last);
        }
    }

    static int getNextInt(Reader in) throws IOException {
        int c;
        boolean negative = false;
        do {
            c = in.read();
            if (!Character.isDigit(c)) {
                negative = c == '-';
            }
        } while (c != -1 && !Character.isDigit(c));
        if (c == -1) return Integer.MIN_VALUE;

        int num = Character.getNumericValue(c);
        while ((c = in.read()) != -1 && Character.isDigit(c)) {
            num = 10 * num + Character.getNumericValue(c);
        }
        return negative ? -num : num;
    }
}

两个选项都不会从内存中传递(((

enter image description here

编辑2我尝试分析

int number = getRandom(); 并从 1000000 开始

enter image description here

再次推出同样的 enter image description here

和飞溅GC

enter image description here

最佳答案

您可以一次从 in 读取一个字符,检查它是否是数字,然后将其累加为一个数字。像这样的东西:

int getNextInt(Reader in) throws IOException {
  int c;
  boolean negative = false;
  do {
    c = in.read();
    if (!Character.isDigit(c)) { negative = c == '-' };
  } while (c != -1 && !Character.isDigit(c));
  if (c == -1) return Integer.MIN_VALUE;  // Some sentinel to indicate nothing found.

  int num = Character.getNumericValue(c);
  while ((c = in.read()) != -1 && Character.isDigit(c)) {
    num = 10 * num + Character.getNumericValue(c);
  }
  return negative ? -num : num;
}

Ideone demo

当然,这是令人难以置信的原始解析。但您也许可以将此代码作为基础并根据需要进行调整。

关于java - 更有效地从控制台读取 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54323425/

相关文章:

file - 如何一次在Hadoop中使用MapReduce处理多个文本文件进行分析

jQuery - 从动态创建的输入中获取值

c - 如何从用户那里获取数字和字符?

c++ - 用 cin 检测输入结束

java - 将具有依赖项的 Java 库打包到 OSGi/Eclipse jar 中

java - 如何设置 JButton 的大小?

java - 在我的 WSL Kali linux 上安装 jdk 时出现语法错误

java - 我想知道如何从数组中删除某些元素

c - 使终端输入在一定数量的字符后发送

python:为多个问题导入随机数