为什么需要使用“big = big.add(..)”来对BigIntegers求和?

时间:2023-01-01 05:29:33

I am a beginner. This might be a silly question.

我是初学者。这可能是一个愚蠢的问题。

I have an array of really big numbers. I need to find the sum of all those numbers in the array. I defined a BigInteger and initialised it to zero. Now I will traverse the array and add each element to this BigInteger.

我有一大堆非常大的数字。我需要找到数组中所有这些数字的总和。我定义了一个BigInteger并将其初始化为零。现在我将遍历数组并将每个元素添加到此BigInteger。

BigInteger big = BigInteger.ZERO;
for(BigInteger b : array){
   big.add(b);
}

No compilation error but big value was still zero,the code didn't work. So, I checked it up and learnt BigInteger add method returns the sum. I modified above code.

没有编译错误但是大值仍然为零,代码不起作用。所以,我检查了一下并学习了BigInteger add方法返回的总和。我修改了上面的代码。

big = big.add(b);

Now this worked fine.

现在这很好用。

My Question: What actually is happening there? Why didn't the first code update big value.

我的问题:那里到底发生了什么?为什么第一个代码没有更新大值。

Can I compare this BigInteger.add() with collection.add()

我可以将此BigInteger.add()与collection.add()进行比较吗?

More insight is appreciated. Thank you.

我们非常感谢您的见解谢谢。

3 个解决方案

#1


8  

Why didn't the first code update big value.

为什么第一个代码没有更新大值。

BigInteger is immutable, you can't change it any more than you can change a String, or any primitive wrapper.

BigInteger是不可变的,您不能更改它,只能更改String或任何原始包装器。

e.g.

String s = "Hello ";
s.concat("World"); // doesn't change anything.

s = s.concat("World"); // Updates 's'

Can I compare this BigInteger.add() with collection.add()

我可以将此BigInteger.add()与collection.add()进行比较吗?

Collections are mutable, but this scalar value is not.

集合是可变的,但这个标量值不是。

Using mutable objects is largely a performance concession. If you have a collection which takes a complete copy each time it would perform very badly.

使用可变对象在很大程度上是一种性能让步。如果你有一个集合,每次它执行非常糟糕时需要一个完整的副本。

#2


4  

This is the JavaDoc for the method

这是该方法的JavaDoc

public BigInteger add(BigInteger val)
Returns a BigInteger whose value is (this + val).

Parameters:
val - value to be added to this BigInteger.

Returns:
this + val

This means that rather than modifying the value, it calculates a new value and returns it. When you perform big = big.add(b), you're running that method, taking the resultant value, and replacing the original value of big with it.

这意味着它不是修改值,而是计算新值并返回它。当你执行big = big.add(b)时,你正在运行该方法,获取结果值,并用它替换big的原始值。

Consider the equivalent using ints, x and y.

考虑使用整数,x和y的等价物。

int x = 3;
int y = 4;
x + y; // At this point, x is still 3 - as you've not assigned the result of this calculation anywhere
x = x + y; // Now - x will be 7

#3


2  

My Question: What actually is happening there? Why didn't the first code update big value.

我的问题:那里到底发生了什么?为什么第一个代码没有更新大值。

Because it's Immutable arbitrary-precision integers means it won't actually change the original one but create a new one when you call add method.Note immutable means for which once the Object is created it's state can not be changed.For example String,Integer,Float etc.

因为它是不可变的任意精度整数意味着它实际上不会改变原始的整数而是在你调用add方法时创建一个新的。注意一旦创建了Object就不能改变它的状态是不可变的。例如String,Integer ,漂浮等

Integer i = new Integer(10);//State 1
i = new Integer(20);//State 2 but does not update state 1

big.add(b); returns value after addition which you need to store in other or same variable.

big.add(b)中;添加后返回值,您需要存储在其他或相同的变量中。

See what add method is doing here,

看看这里有什么add方法,

public BigInteger add(BigInteger val) {
    if (val.signum == 0)
        return this;
    if (signum == 0)
        return val;
    if (val.signum == signum)
        return new BigInteger(add(mag, val.mag), signum);

    int cmp = compareMagnitude(val);
    if (cmp == 0)
        return ZERO;
    int[] resultMag = (cmp > 0 ? subtract(mag, val.mag)
                       : subtract(val.mag, mag));
    resultMag = trustedStripLeadingZeroInts(resultMag);
    //Here it's creating new Object
    return new BigInteger(resultMag, cmp == signum ? 1 : -1);//<====
}

Can I compare this BigInteger.add() with collection.add()

我可以将此BigInteger.add()与collection.add()进行比较吗?

Here say List.add will actually add the element to the list and you can change the value of that element and note List.add does not create a new element but it actually adds the reference of original element.

这里说List.add实际上会将元素添加到列表中,您可以更改该元素的值并注意List.add不会创建新元素,但它实际上添加了原始元素的引用。

#1


8  

Why didn't the first code update big value.

为什么第一个代码没有更新大值。

BigInteger is immutable, you can't change it any more than you can change a String, or any primitive wrapper.

BigInteger是不可变的,您不能更改它,只能更改String或任何原始包装器。

e.g.

String s = "Hello ";
s.concat("World"); // doesn't change anything.

s = s.concat("World"); // Updates 's'

Can I compare this BigInteger.add() with collection.add()

我可以将此BigInteger.add()与collection.add()进行比较吗?

Collections are mutable, but this scalar value is not.

集合是可变的,但这个标量值不是。

Using mutable objects is largely a performance concession. If you have a collection which takes a complete copy each time it would perform very badly.

使用可变对象在很大程度上是一种性能让步。如果你有一个集合,每次它执行非常糟糕时需要一个完整的副本。

#2


4  

This is the JavaDoc for the method

这是该方法的JavaDoc

public BigInteger add(BigInteger val)
Returns a BigInteger whose value is (this + val).

Parameters:
val - value to be added to this BigInteger.

Returns:
this + val

This means that rather than modifying the value, it calculates a new value and returns it. When you perform big = big.add(b), you're running that method, taking the resultant value, and replacing the original value of big with it.

这意味着它不是修改值,而是计算新值并返回它。当你执行big = big.add(b)时,你正在运行该方法,获取结果值,并用它替换big的原始值。

Consider the equivalent using ints, x and y.

考虑使用整数,x和y的等价物。

int x = 3;
int y = 4;
x + y; // At this point, x is still 3 - as you've not assigned the result of this calculation anywhere
x = x + y; // Now - x will be 7

#3


2  

My Question: What actually is happening there? Why didn't the first code update big value.

我的问题:那里到底发生了什么?为什么第一个代码没有更新大值。

Because it's Immutable arbitrary-precision integers means it won't actually change the original one but create a new one when you call add method.Note immutable means for which once the Object is created it's state can not be changed.For example String,Integer,Float etc.

因为它是不可变的任意精度整数意味着它实际上不会改变原始的整数而是在你调用add方法时创建一个新的。注意一旦创建了Object就不能改变它的状态是不可变的。例如String,Integer ,漂浮等

Integer i = new Integer(10);//State 1
i = new Integer(20);//State 2 but does not update state 1

big.add(b); returns value after addition which you need to store in other or same variable.

big.add(b)中;添加后返回值,您需要存储在其他或相同的变量中。

See what add method is doing here,

看看这里有什么add方法,

public BigInteger add(BigInteger val) {
    if (val.signum == 0)
        return this;
    if (signum == 0)
        return val;
    if (val.signum == signum)
        return new BigInteger(add(mag, val.mag), signum);

    int cmp = compareMagnitude(val);
    if (cmp == 0)
        return ZERO;
    int[] resultMag = (cmp > 0 ? subtract(mag, val.mag)
                       : subtract(val.mag, mag));
    resultMag = trustedStripLeadingZeroInts(resultMag);
    //Here it's creating new Object
    return new BigInteger(resultMag, cmp == signum ? 1 : -1);//<====
}

Can I compare this BigInteger.add() with collection.add()

我可以将此BigInteger.add()与collection.add()进行比较吗?

Here say List.add will actually add the element to the list and you can change the value of that element and note List.add does not create a new element but it actually adds the reference of original element.

这里说List.add实际上会将元素添加到列表中,您可以更改该元素的值并注意List.add不会创建新元素,但它实际上添加了原始元素的引用。