指南要求以下内容:
BigIntegers will be represented with 50 digit arrays of int (where each integer in the array is an integer in the range 0..9).
You will have a class called BigInteger that has the following methods:
BigInteger( ) --- initialize the BigInteger to 0
BigInteger(int n) --- initialize the BigInteger to the value of n
BigInteger( BigInteger n) --- a copy constructor
我的问题是,最有效的方法是什么?目前,我有:
public class BigInteger {
int[] BigInteger = new int[50];
public BigInteger() {
for(int i = 0; i < BigInteger.length; i++) {
BigInteger[i] = 0;
}
}
这似乎有效,但仅用于将数组初始化为0 ....我已经检查了Stack Overflow,但是空出来了。有人能指出我如何解决这个问题吗?
答案 0 :(得分:0)
我不是一个java家伙,但那是怎么回事。
public BigInteger() {
for(int i = 0; i < BigInteger.length; i++) {
BigInteger[i] = 0;
}
}
public BigInteger(BigInteger bigInteger) {
for(int i = 0; i < BigInteger.length; i++) {
BigInteger[i] = bigInteger[i];
}
}
public BigInteger(int n) {
String nstr = n.toString(); // not sure
int pos = 49;
for(int i = nstr.length - 1; i >= 0 ; i--) {
BigInteger[pos] = Integer.parse(nstr [i]); // parse each char, you get the idea
pos--;
}
}
感谢@Andreas。