gpt4 book ai didi

java - 内存不足错误 : Java heap space when trying to read 5 ints into an array

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:39:20 26 4
gpt4 key购买 nike

<分区>

我正在练习 Java 新鲜面试编码示例。我正在尝试编写一个程序来查找 1N 之间的重复数字,其中 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堆空间错误?代码编译并运行良好。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com