OutOfMemoryError: Java heap space when trying to read 5 ints into an array
OutOfMemoryError: Java heap space when trying to read 5 ints into an array
我正在练习 Java 更新鲜的面试编码示例。
我正在尝试编写一个程序来查找 1
到 N
之间的重复数字,其中 N 由用户与数字本身一起给出。
这是代码:
import java.io.DataInputStream;
import java.io.IOException;
public class DuplicateNumbers {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(System.in);
System.out.println(" Enter the number of numbers ");
int a = in.readInt();
int[] num = new int[a];
System.out.println(" Enter the ints one by one ");
for (int b = 0; b < a; b++) {
System.out.println(" Enter no "+(b+1));
num[b]=in.readInt();
}
int c = 0;
for (int d = 0; d < a; d++) {
int f = 0;
c = num[d];
for (int e=0; e<a; e++) {
if (c==num[e]) {
f++;
}
}
if(f > 1)
System.out.println(" Duplicate number "+c);
}
}
}
但我在 Eclipse Neon 中遇到以下错误:
Enter the number of numbers
5
Exception in thread "main" java.lang.OutOfMemoryError:
Java heap space at DuplicateNumbers.main(DuplicateNumbers.java:14)
怎么了?为什么 JVM 堆 space 错误?
代码编译并运行良好。
DataInputStream 适用于二进制而非文本。当您键入 4 个字节时,这将变成一个 32 位 int
值,例如5, \n, \n, \n 大约是 9 亿,这就是它在创建数组时抱怨内存不足的原因。您可以通过单步执行调试器中的代码来检查这一点。
你需要的是文本输入,试试用
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of numbers");
int a = in.nextInt();
in.nextLine(); // discard the rest of the line.
从这里开始:
DataInputStream in=new DataInputStream(System.in);
你不应该使用 DataInputStream
...我看到你已经得到了解释。
但除此之外:
for(int e=0; e<a; e++)
你会立即运行 num[d] 和 num[e] 相等。因为您的第二个循环实际上将 num[0] 与 num[0] 进行了比较。所以:第二个循环只需要 运行 在外层循环之后的索引上!
除此之外,还不清楚您是否真的想要 50 次 "duplicate" 以防您输入了 50 次相同的数字。我宁愿去打印 number 个重复的数字。
换句话说:在修复输入流问题后,您的代码仍然无法执行正确的操作。
除此之外:您对 single-character 名称的使用使得几乎不可能轻易理解这段代码的作用。
尝试为您的 class 使用 Scanner。
这是一个基本示例:
public static void main(String[] args) throws IOException
{
System.out.println("Enter a number: ");
Scanner sc = new Scanner(System.in);
String item = sc.next();
System.out.println("Your item is: " + item);
sc.close();
}
我正在练习 Java 更新鲜的面试编码示例。
我正在尝试编写一个程序来查找 1
到 N
之间的重复数字,其中 N 由用户与数字本身一起给出。
这是代码:
import java.io.DataInputStream;
import java.io.IOException;
public class DuplicateNumbers {
public static void main(String[] args) throws IOException {
DataInputStream in = new DataInputStream(System.in);
System.out.println(" Enter the number of numbers ");
int a = in.readInt();
int[] num = new int[a];
System.out.println(" Enter the ints one by one ");
for (int b = 0; b < a; b++) {
System.out.println(" Enter no "+(b+1));
num[b]=in.readInt();
}
int c = 0;
for (int d = 0; d < a; d++) {
int f = 0;
c = num[d];
for (int e=0; e<a; e++) {
if (c==num[e]) {
f++;
}
}
if(f > 1)
System.out.println(" Duplicate number "+c);
}
}
}
但我在 Eclipse Neon 中遇到以下错误:
Enter the number of numbers
5
Exception in thread "main" java.lang.OutOfMemoryError:
Java heap space at DuplicateNumbers.main(DuplicateNumbers.java:14)
怎么了?为什么 JVM 堆 space 错误? 代码编译并运行良好。
DataInputStream 适用于二进制而非文本。当您键入 4 个字节时,这将变成一个 32 位 int
值,例如5, \n, \n, \n 大约是 9 亿,这就是它在创建数组时抱怨内存不足的原因。您可以通过单步执行调试器中的代码来检查这一点。
你需要的是文本输入,试试用
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of numbers");
int a = in.nextInt();
in.nextLine(); // discard the rest of the line.
从这里开始:
DataInputStream in=new DataInputStream(System.in);
你不应该使用 DataInputStream
...我看到你已经得到了解释。
但除此之外:
for(int e=0; e<a; e++)
你会立即运行 num[d] 和 num[e] 相等。因为您的第二个循环实际上将 num[0] 与 num[0] 进行了比较。所以:第二个循环只需要 运行 在外层循环之后的索引上!
除此之外,还不清楚您是否真的想要 50 次 "duplicate" 以防您输入了 50 次相同的数字。我宁愿去打印 number 个重复的数字。
换句话说:在修复输入流问题后,您的代码仍然无法执行正确的操作。
除此之外:您对 single-character 名称的使用使得几乎不可能轻易理解这段代码的作用。
尝试为您的 class 使用 Scanner。
这是一个基本示例:
public static void main(String[] args) throws IOException
{
System.out.println("Enter a number: ");
Scanner sc = new Scanner(System.in);
String item = sc.next();
System.out.println("Your item is: " + item);
sc.close();
}