0

如果我下面的代码不在 for() 循环中,当我的“n2”值超过 7853 时,我很难理解为什么会出现 StackOverflowError。

这是代码:

package tp3;

import java.util.Date;
public class TP3 {



//Somme des fractions Récursive
public static double somFractionsRecursive(double index) throws IllegalArgumentException{
    if (index == 0) throw new IllegalArgumentException("Erreur: impossible de diviser par zéro");
    if (index == 1) return 1/index;

    double reponse = (1/index) + somFractionsRecursive(index-1);


    return reponse;
}

//Somme des fractions itérative
public static double somFractionsIterative(double index) throws IllegalArgumentException{
if (index == 0) throw new IllegalArgumentException("Erreur: impossible de diviser par zéro");
double reponse = 0;
for (double i = 1; i <= index; i++) {
 reponse += 1/i ;
}
    return reponse;
}


public static void main(String[] args) {
    Date d = new Date();


    double n1 = 999, n2 = 7853;

    System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en récursivité: ");
    d = new Date();
    long debut = d.getTime();
    System.out.println("somFractionsRecursive("+n1+") = " + somFractionsRecursive(n1));
    d = new Date();
    long fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");


    System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en récursivité: ");
    d = new Date();
    debut = d.getTime();
    System.out.println("somFractionsRecursive("+n2+") = " + somFractionsRecursive(n2));
    d = new Date();
    fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");


    System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en itération: ");
    d = new Date();
    debut = d.getTime();
    System.out.println("somFractionsIterative("+n1+") = " + somFractionsIterative(n1));
    d = new Date();
    fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");


    System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en itération: ");
    d = new Date();
    debut = d.getTime();
    System.out.println("somFractionsIterative("+n2+") = " + somFractionsIterative(n2));
    d = new Date();
    fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");

  }// End Main
}// End TP3 Class

但是,我将最后一段代码放在 for() 循环中,我可以毫无问题地获得高达 9999 的“n2”变量

以下代码没有给我 StackOverflowError:

 // Functions are the same as above...
 System.out.println("\n\nTemps requis pour trouvers somFraction("+n1+") en itération: ");
    d = new Date();
    debut = d.getTime();
    for (int i = 1; i <= n1; i++) {
        System.out.println("somFractionsIterative("+i+") = " + somFractionsIterative(i));
    }
    d = new Date();
    fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");


    System.out.println("\n\nTemps requis pour trouvers somFraction("+n2+") en itération: ");
    d = new Date();
    debut = d.getTime();
    for (int i = 1; i <= n2; i++) {
        System.out.println("somFractionsIterative("+i+") = " + somFractionsIterative(i));
    }
    d = new Date();
    fin = d.getTime();
    System.out.println("Terminé! Temps requis: "+((fin-debut))+" milisecondes");

对不起,我对此很陌生!并一如既往地感谢您的帮助:)

4

2 回答 2

1

递归调用每次迭代的方法,而迭代不会。由于您正在调用一个新方法,因此会创建一个新的堆栈帧并将其推送到堆栈上。如果您正在迭代,您可能会永远走下去(无限循环),而使用递归,您会不断消耗更多内存。本教程可以帮助您了解堆栈。http://www.javatutorialhub.com/java-stack-heap.html

于 2013-04-01T21:44:44.390 回答
0

由于其主体中的递归,您一次又一次地调用 somFractionsRecursive。堆栈内存不是无限的,因此您会遇到异常。

于 2013-04-01T21:48:47.927 回答