Java-更改的斐波那契兔子-Java堆空间错误

Java-更改的斐波那契兔子-Java堆空间错误,java,fibonacci,Java,Fibonacci,这就是情况——斐波那契通过研究一对每月繁殖的兔子和另一对将在下个月繁殖的兔子,开发了他的递归序列f(n)=f(n-1)+f(n-2)。 我正在改变这种情况,使它们不会在第一个月后繁殖,而是在第二个月后繁殖。 我输出的数字是正确的。然而,我的问题是,45个月后,由于某种原因,我得到了一个Java堆空间错误。 我试过用一个长的和一个双的来代替,但是没有用。请让我知道你的想法。 代码: 和输出: --------------------Configuration: <Default>---

这就是情况——斐波那契通过研究一对每月繁殖的兔子和另一对将在下个月繁殖的兔子,开发了他的递归序列f(n)=f(n-1)+f(n-2)。 我正在改变这种情况,使它们不会在第一个月后繁殖,而是在第二个月后繁殖。 我输出的数字是正确的。然而,我的问题是,45个月后,由于某种原因,我得到了一个Java堆空间错误。 我试过用一个长的和一个双的来代替,但是没有用。请让我知道你的想法。 代码:

和输出:

--------------------Configuration: <Default>--------------------
Months: 1 Population: 1
Months: 2 Population: 1
Months: 3 Population: 2
Months: 4 Population: 3
Months: 5 Population: 4
Months: 6 Population: 6
Months: 7 Population: 9
Months: 8 Population: 13
Months: 9 Population: 19
Months: 10 Population: 28
Months: 15 Population: 189
Months: 20 Population: 1278
Months: 25 Population: 8641
Months: 30 Population: 58425
Months: 35 Population: 395033
Months: 40 Population: 2670964
Months: 45 Population: 18059374
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:411)
    at Rabbits.main(Rabbits.java:22)

Process completed.
--------------配置:--------------------
月份:1人口:1
月份:2人口:1
月份:3人口:2
月份:4人口:3
月份:5人口:4
月份:6人口:6
月份:7人口:9
月份:8人口:13
月份:9人口:19
月份:10人口:28
月份:15人口:189
月份:20人口:1278
月份:25人口:8641
月份:30人口:58425
月份:35人口:395033
月份:40人口:2670964
月份:45人口:18059374
线程“main”java.lang.OutOfMemoryError中出现异常:java堆空间
位于java.util.Arrays.copyOf(Arrays.java:2245)
位于java.util.Arrays.copyOf(Arrays.java:2219)
在java.util.ArrayList.grow(ArrayList.java:213)
位于java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
在java.util.ArrayList.add(ArrayList.java:411)处
at rabbts.main(rabbes.java:22)
进程已完成。

不幸的是,您目前的做法行不通。为每个兔子保留一个整数。由于兔子的数量迅速增加,它们的数量将不会成为一个阵列中容纳每只兔子的方式

因为您只需要知道兔子的年龄,所以可以保留代表某个年龄段兔子数量的变量。随着每个月的过去,所有兔子的年龄都在增加,新兔子出生的年龄为0岁

通过这种方式,您可以跟踪成熟兔子的数量,以及每个年龄组中有多少只兔子

最终的种群将是不同年龄组的所有兔子的总和

Scanner in = new Scanner(new File("src/q22565464/rabbits.dat"));
int numSets = Integer.parseInt(in.nextLine().trim());

for(int curSet = 0; curSet<numSets; curSet++){
   int months = Integer.parseInt(in.nextLine().trim());
   // Could be converted to an array, left for clarity's sake
   long matured = 0;
   long age2 = 0;
   long age1 = 0;
   long age0 = 1;

   for(int i = 0; i < months; i++){
    // the ages of the rabbits increase with each month
       matured += age2;
       age2 = age1;
       age1 = age0;
       age0 = matured;
   }
   System.out.println("Months: " + months + " Population: " + (matured+age2+age1+age0));
}
in.close();
Scanner in=new Scanner(新文件(“src/q22565464/rabbts.dat”);
int numSets=Integer.parseInt(in.nextLine().trim());

对于(int curSet=0;curSetTry将您的计数保持为长并递增,而不是拥有庞大的数字0数组,这些数字将被一次又一次地复制…我不知道我怎么做。我必须跟踪每只兔子,以确保它们在3个月大时开始繁殖。您可以这样做通过创建一个公式来描述每一代的前身。试图跟踪每一只兔子的年龄需要更多的内存——可能在TB的数量级。我印象深刻。你是怎么做到的?每当我遇到数据太多的问题时,我都会找出我不需要的。在你的ca中se,你可以看到,你不需要在个人层面上跟踪每只兔子。如果你想练习这样的问题,试试。哇,这是一个很棒的工具。我一定会使用它。非常感谢。
--------------------Configuration: <Default>--------------------
Months: 1 Population: 1
Months: 2 Population: 1
Months: 3 Population: 2
Months: 4 Population: 3
Months: 5 Population: 4
Months: 6 Population: 6
Months: 7 Population: 9
Months: 8 Population: 13
Months: 9 Population: 19
Months: 10 Population: 28
Months: 15 Population: 189
Months: 20 Population: 1278
Months: 25 Population: 8641
Months: 30 Population: 58425
Months: 35 Population: 395033
Months: 40 Population: 2670964
Months: 45 Population: 18059374
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2245)
    at java.util.Arrays.copyOf(Arrays.java:2219)
    at java.util.ArrayList.grow(ArrayList.java:213)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:187)
    at java.util.ArrayList.add(ArrayList.java:411)
    at Rabbits.main(Rabbits.java:22)

Process completed.
Scanner in = new Scanner(new File("src/q22565464/rabbits.dat"));
int numSets = Integer.parseInt(in.nextLine().trim());

for(int curSet = 0; curSet<numSets; curSet++){
   int months = Integer.parseInt(in.nextLine().trim());
   // Could be converted to an array, left for clarity's sake
   long matured = 0;
   long age2 = 0;
   long age1 = 0;
   long age0 = 1;

   for(int i = 0; i < months; i++){
    // the ages of the rabbits increase with each month
       matured += age2;
       age2 = age1;
       age1 = age0;
       age0 = matured;
   }
   System.out.println("Months: " + months + " Population: " + (matured+age2+age1+age0));
}
in.close();