我正在用 Java 编写自己的自定义 BigInteger 类,并且想要在我的类的构造函数中解析整数。所以问题是,如何将数字 n 的每个数字正确添加到我的向量中,并保持正确的顺序?换句话说,如何将每个数字添加到其中,就像将它们添加到堆栈中一样?
例如,n = 1234我需要将 1 2 3 4 添加到我的向量中。
这就是我已经拥有的:
class VeryLong {
Vector<Integer> A = new Vector<Integer>();
VeryLong(int n) {
while (n > 0) {
// A.push(n % 10)
n /= 10;
}
}
还有另一个问题,我需要重载该类的构造函数以从 int 和 long 创建 VeryLong 的实例。这是我的代码:
private ArrayList<Long> A = new ArrayList<>();
private VeryLong(int n) {
while (n > 0) {
A.add(long()(n % 10));
n /= 10;
}
while (!A.isEmpty()) {
System.out.println(A.get(0));
A.remove(0);
}
}
private VeryLong(long n) {
while (n > 0) {
A.add(n % 10);
n /= 10;
}
while (!A.isEmpty()) {
System.out.println(A.get(0));
A.remove(0);
}
}
如果我定义了构造ArrayList函数Long的第一个构造函数,就会出现错误。add()同样,如果我定义A为,那么第二个方法就会出错Vector<Integer> A = new Vector<Integer>();。我该如何修复它?
qq_遁去的一_1
相关分类