将元素推送到 java 中的 Vector
Pushing elements to Vector in java
我正在 Java 中编写自己的自定义 BigInteger class,并希望在 class 的构造函数中解析整数。所以问题是,如何将我的数字 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;
}
}
还有另一个问题,我需要重载 class 的构造函数以从 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);
}
}
如果我定义 Long
的 ArrayList
,构造函数第一个构造函数就会出错。同样,如果我将 A
定义为 Vector<Integer> A = new Vector<Integer>();
,则第二个 add()
方法出错。我该如何解决?
通过快速查看the Javadoc,没有push
方法。但是,我认为您正在寻找的是 add
方法,它将给定的项目添加到 Vector
的末尾(或者如果提供了额外的整数,则在 Vector
).在您的示例中,这看起来像
class VeryLong {
Vector<Integer> A = new Vector<Integer>();
VeryLong(int n) {
while (n > 0) {
A.add(0, n % 10);
n /= 10;
}
}
在这种情况下,我写了 A.add(0, n % 10);
,因为您希望最后有 "less significant" 个数字。在这种情况下,添加的每个连续数字都会将现有元素推到列表的 "right" 或末尾。这应该可以解决您的问题。 :)
正如 acarlstein 指出的那样,在这种情况下不一定推荐使用 Vector
。引用自 Vector
Javadoc,
As of the Java 2 platform v1.2, this class was retrofitted to implement the List
interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector
is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList
in place of Vector
.
我正在 Java 中编写自己的自定义 BigInteger class,并希望在 class 的构造函数中解析整数。所以问题是,如何将我的数字 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;
}
}
还有另一个问题,我需要重载 class 的构造函数以从 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);
}
}
如果我定义 Long
的 ArrayList
,构造函数第一个构造函数就会出错。同样,如果我将 A
定义为 Vector<Integer> A = new Vector<Integer>();
,则第二个 add()
方法出错。我该如何解决?
通过快速查看the Javadoc,没有push
方法。但是,我认为您正在寻找的是 add
方法,它将给定的项目添加到 Vector
的末尾(或者如果提供了额外的整数,则在 Vector
).在您的示例中,这看起来像
class VeryLong {
Vector<Integer> A = new Vector<Integer>();
VeryLong(int n) {
while (n > 0) {
A.add(0, n % 10);
n /= 10;
}
}
在这种情况下,我写了 A.add(0, n % 10);
,因为您希望最后有 "less significant" 个数字。在这种情况下,添加的每个连续数字都会将现有元素推到列表的 "right" 或末尾。这应该可以解决您的问题。 :)
正如 acarlstein 指出的那样,在这种情况下不一定推荐使用 Vector
。引用自 Vector
Javadoc,
As of the Java 2 platform v1.2, this class was retrofitted to implement the
List
interface, making it a member of the Java Collections Framework. Unlike the new collection implementations,Vector
is synchronized. If a thread-safe implementation is not needed, it is recommended to useArrayList
in place ofVector
.