import java.math.BigInteger;
public class PascalsTriangle {
public int row = 0;
public BigInteger[][] pascal = new BigInteger[ (int) (row +1)][];
public PascalsTriangle(int dimension) {
row = dimension + 2;
pascal = new BigInteger[row +1][];
pascal[1] = new BigInteger[1 + 2];
pascal[1][1] = new BigInteger("1");
for (int i = 2; i <= row; i++) {
pascal[i] = new BigInteger[i + 2];
for (int j = 1; j < pascal[i].length - 1; j++) {
pascal[i][j] = pascal[i - 1][j - 1].add(pascal[i-1][j]) ;
}
}
}
public BigInteger getface(int row, int column) {
return pascal[row][column];
}
}
我试图打印一个帕斯卡三角形,但对我的整数要求非常高,即超过 的最大值long
,所以我使用了 BigInteger。但是我在 PascalsTriangle 类中添加 BigIntegers 的部分得到了 NullPointerException。这是添加二维 BigInteger 数组的正确方法吗?