Reputation: 834
I've come across a situation where I have to perform sum and multiplication of the elements of an ArrayList
and for this normally I use:
sum+=a.get(i)*b.get(i);
which gives me the exact output I want, but here in my case I've got to do the same with ArrayList
of BigInteger
type, I've tried it with the below statement, but it is not working as expected.
sum=(a.get(i).multiply(b.get(i))).add(sum);
please let me know how I can do it.
Thanks.
Upvotes: 1
Views: 5472
Reputation: 1263
Your question is extremely unclear, and as such shouldn't warrant an answer. However, see if the following might help you.
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));
}
Also ensure that you are not falling into the following pit. BigInteger is immutable.
Upvotes: 2
Reputation: 67
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);
Upvotes: 0