使用BigInteger的和和乘法

时间:2014-02-25 08:55:34

标签: java arraylist biginteger

我遇到过一种情况,我必须对ArrayList的元素进行求和和乘法,这通常是我使用的:

sum+=a.get(i)*b.get(i);

它给了我想要的确切输出,但在我的情况下,我必须对ArrayList BigInteger类型的sum=(a.get(i).multiply(b.get(i))).add(sum); 做同样的事情,我已尝试使用以下语句,但它没有按预期工作。

{{1}}

请让我知道我该怎么做。

感谢。

2 个答案:

答案 0 :(得分:1)

你的问题非常不清楚,因此不应该得到答案。但是,请查看以下内容是否对您有所帮助。

public static void main(String[] args) {
    BigInteger b1 = new BigInteger("1000"), b2 = new BigInteger("1000");
    System.out.println("b1:      " + b1);
    System.out.println("b1 * b2: "+ b1.multiply(b2));
}

同时确保您不会落入following pit。 BigInteger是不可变的。

答案 1 :(得分:0)

BigInteger sum = new BigInteger("10");
List<BigInteger> list = new ArrayList<BigInteger>();
list.add(new BigInteger("2"));
list.add(new BigInteger("3"));
sum=(list.get(0).multiply(list.get(1))).add(sum);
System.out.println(sum);