迭代地添加 ArrayDeque 中的所有元素
Adding all elements in an ArrayDeque Iteratively
我有一个 BigInts 的 arrayDeque,我将其实现为基本上只是保存字符串 IE BigInt@Instancewhatever = "3476234236734567"
我已经有一种方法可以将一个 BigInt 添加到另一个 returns 一个新的 BigInt,其中包含两个 BigInt 之和的字符串。
即
BigInt@1 = "4321"
BigInt@2 = "5555"
BigInt@Sum = "9876"
我的问题是如何遍历此 Deque 并在 BigInts 上调用 add。
我正在考虑对 ArrayDeque 的大小使用 forloop,但 ArrayDeque 并没有真正的 x.get(x.size()-1)
功能,就像您使用普通 ArrayList 那样。
编辑:为了进一步推断,这是我目前正在使用的。
digit是字符串IE的List格式
"1,2,3,4","5,5,5,5"
等
public BigInt times(BigInt operand){
List<Integer> a = this.getDigit();
List<Integer> b = operand.getDigit();
//sum left unused atm
List<Integer> sum = operand.getDigit();
Deque<BigInt> temp = new ArrayDeque<>();
Deque<BigInt> temp1 = new ArrayDeque<>();
if(a.size() > b.size()){
temp1 = multiply(a,b,temp);
//Iterate here?
} else {
temp1 = multiply(b,a,temp);
//Iterate here?
}
return new BigInt(temp1.toString());
}
ArrayDeque<T>
实现了 Iterable<T>
,因此您可以对其使用 for-each 循环:
ArrayDeque<BigInt> deque = new ArrayDeque<BigInt>();
//populate deque
BigInt sum = new BigInt("0");
for(BigInt b : deque) {
sum = sum.add(b);
}
//sum now holds the sum of the elements in deque.
这假设您的 BigInt
class 有一个方法 add(BigInt b)
。要么你已经实现了它,要么你打算使用 BigInteger
,内置的 class 用于大整数。
总结双端队列中元素的一种更高级的方法是使用流归约操作和方法引用:
ArrayDeque<BigInt> deque = new ArrayDeque<>();
//populate deque...
BigInteger sum = deque.stream().reduce(BigInt::add).orElseGet(() -> new BigInt("0"));
ArrayDeque
实现了 Iterable
,因此您可以使用扩展的 for 循环:
BigInteger sum = BigInteger.ZERO;
for (BigInteger value : arrayDeque) {
sum = sum.add(value);
}
我有一个 BigInts 的 arrayDeque,我将其实现为基本上只是保存字符串 IE BigInt@Instancewhatever = "3476234236734567"
我已经有一种方法可以将一个 BigInt 添加到另一个 returns 一个新的 BigInt,其中包含两个 BigInt 之和的字符串。 即
BigInt@1 = "4321"
BigInt@2 = "5555"
BigInt@Sum = "9876"
我的问题是如何遍历此 Deque 并在 BigInts 上调用 add。
我正在考虑对 ArrayDeque 的大小使用 forloop,但 ArrayDeque 并没有真正的 x.get(x.size()-1)
功能,就像您使用普通 ArrayList 那样。
编辑:为了进一步推断,这是我目前正在使用的。
digit是字符串IE的List格式
"1,2,3,4","5,5,5,5"
等
public BigInt times(BigInt operand){
List<Integer> a = this.getDigit();
List<Integer> b = operand.getDigit();
//sum left unused atm
List<Integer> sum = operand.getDigit();
Deque<BigInt> temp = new ArrayDeque<>();
Deque<BigInt> temp1 = new ArrayDeque<>();
if(a.size() > b.size()){
temp1 = multiply(a,b,temp);
//Iterate here?
} else {
temp1 = multiply(b,a,temp);
//Iterate here?
}
return new BigInt(temp1.toString());
}
ArrayDeque<T>
实现了 Iterable<T>
,因此您可以对其使用 for-each 循环:
ArrayDeque<BigInt> deque = new ArrayDeque<BigInt>();
//populate deque
BigInt sum = new BigInt("0");
for(BigInt b : deque) {
sum = sum.add(b);
}
//sum now holds the sum of the elements in deque.
这假设您的 BigInt
class 有一个方法 add(BigInt b)
。要么你已经实现了它,要么你打算使用 BigInteger
,内置的 class 用于大整数。
总结双端队列中元素的一种更高级的方法是使用流归约操作和方法引用:
ArrayDeque<BigInt> deque = new ArrayDeque<>();
//populate deque...
BigInteger sum = deque.stream().reduce(BigInt::add).orElseGet(() -> new BigInt("0"));
ArrayDeque
实现了 Iterable
,因此您可以使用扩展的 for 循环:
BigInteger sum = BigInteger.ZERO;
for (BigInteger value : arrayDeque) {
sum = sum.add(value);
}