我需要重载 BigInteger 类的构造函数,以从 int
创建 VeryLong 的实例。和long
。这是我的代码:
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);
}
}
如果我将 A 定义为 ArrayList
的Long
第一个构造函数出现错误。同样,add()
中的错误第二个方法,如果我定义 A
如Vector<Integer> A = new Vector<Integer>();
。我该如何修复它?
最佳答案
构造函数中的错误是由于错误的转换语法造成的:
应该是A.add((long)(n % 10));
,而不是A.add(long()(n % 10));
关于java - 重载 Java 类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58403667/