递归和栈溢出
递归对内存空间得要求很高,递归很容易造成栈溢出
public static void printN2(int N){
if (N>0){
printN2(N-1);
System.out.printf("%1$-2d",N);
}
}
@Test
public void testPrintN2() throws Exception {
long ms1 = Calendar.getInstance().getTimeInMillis();
Misc.printN2(10000);
long ms2 = Calendar.getInstance().getTimeInMillis();
System.out.println();
System.out.println(ms2-ms1);
}
这样得一个测试程序,在设置N为10000时可以跑完,但在设置N为20000以上就会报栈溢出得错误
java.lang.StackOverflowError
at com.scarlett.bowling.Misc.printN2(Misc.java:15)
at com.scarlett.bowling.Misc.printN2(Misc.java:16)
at com.scarlett.bowling.Misc.printN2(Misc.java:16)