cyp*_*aya 2 java stack-overflow map
import java.math.BigInteger;
import java.util.HashMap;
/**
*
* @author cypronmaya
*/
public class test {
static HashMap<Integer, BigInteger> cache = new HashMap<Integer, BigInteger>();
public static void main(String[] args) {
System.out.println(factorial(20000));
}
public static BigInteger factorial(int n) {
BigInteger ret;
if (n == 0) {
return BigInteger.ONE;
}
if (null != (ret = cache.get(n))) {
return ret;
}
ret = BigInteger.valueOf(n).multiply(factorial(n - 1));
cache.put(n, ret);
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
java.util.HashMap.get中的线程"main"java.lang.StackOverflowError中的异常(未知来源)
嗨,为什么我得到这个程序的stackoverflow异常?
我知道stackoverflow通常意味着你有一个无限循环,但是当我使用10000或其他一些较小的数字时,这种方法可以正常工作,而大数字突然变得无限大?
| 归档时间: |
|
| 查看次数: |
5294 次 |
| 最近记录: |