如果我的“n2”值超过7853,如果下面的代码不在for()循环中,我很难理解为什么我得到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()循环中我可以得到“n2”变量,最高可达9999而没有任何问题
以下代码没有给我一个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");
抱歉这个烂摊子,我对此很新! 并感谢您一如既往的帮助:)
答案 0 :(得分:1)
每次迭代都会对方法进行递归调用,而迭代则不会。由于您正在进行新方法调用,因此会在堆栈上创建并推送新的堆栈帧。如果你正在迭代你可以永远(无限循环)而使用递归你不断消耗更多的内存。本教程可以帮助您理解堆栈。 http://www.javatutorialhub.com/java-stack-heap.html
答案 1 :(得分:0)
由于其正文中的递归,您一次又一次地调用somFractionsRecursive。堆栈内存不是无限制的,因此您将获得异常。