我正在练习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堆空间错误?代码编译和运行都没有问题。
int[] num=new int[a];
这一行崩溃了?你尝试过添加断点并调试代码吗? - luk2302