为什么需要使用 "big = big.add(..)" 来求和 BigIntegers?
Why does "big = big.add(..)" need to be used to sum BigIntegers?
我是初学者。这可能是一个愚蠢的问题。
我有一组非常大的数字。我需要找到数组中所有这些数字的总和。我定义了一个 BigInteger 并将其初始化为零。现在我将遍历数组并将每个元素添加到这个 BigInteger.
BigInteger big = BigInteger.ZERO;
for(BigInteger b : array){
big.add(b);
}
没有编译错误,但 big
值仍然为零,代码无效。所以,我查了一下,了解到 BigInteger add method returns the sum。我修改了上面的代码。
big = big.add(b);
现在效果很好。
我的问题:那里到底发生了什么?为什么第一个代码没有更新 big
值。
我可以比较这个 BigInteger.add()
和 collection.add()
感谢您提供更多见解。谢谢。
My Question: What actually is happening there? Why didn't the first
code update big value.
因为它是 不可变的任意精度整数 意味着它实际上不会更改原始整数,而是在您调用 add
method.Note 时创建一个新整数不可变意味着一旦创建 Object
,它的状态就不能是 changed.For 例如 String
、Integer
、Float
等
Integer i = new Integer(10);//State 1
i = new Integer(20);//State 2 but does not update state 1
big.add(b);
returns 添加后的值,您需要将其存储在其他或相同的变量中。
看看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()
这里说 List.add
实际上会将元素添加到列表中,您可以更改该元素的值,注意 List.add
不会创建新元素,但它实际上会添加原始元素的引用元素.
Why didn't the first code update big value.
BigInteger 是不可变的,您无法更改它,就像您无法更改 String 或任何原始包装器一样。
例如
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()
集合是可变的,但这个标量值不是。
使用可变对象在很大程度上是一种性能让步。如果你有一个每次都需要完整副本的集合,它的性能会很差。
这是方法的 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
这意味着它不是修改值,而是计算一个新值并 returns 它。当您执行 big = big.add(b)
时,您就是 运行 那个方法,获取结果值,并用它替换 big 的原始值。
考虑使用整数、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
我是初学者。这可能是一个愚蠢的问题。
我有一组非常大的数字。我需要找到数组中所有这些数字的总和。我定义了一个 BigInteger 并将其初始化为零。现在我将遍历数组并将每个元素添加到这个 BigInteger.
BigInteger big = BigInteger.ZERO;
for(BigInteger b : array){
big.add(b);
}
没有编译错误,但 big
值仍然为零,代码无效。所以,我查了一下,了解到 BigInteger add method returns the sum。我修改了上面的代码。
big = big.add(b);
现在效果很好。
我的问题:那里到底发生了什么?为什么第一个代码没有更新 big
值。
我可以比较这个 BigInteger.add()
和 collection.add()
感谢您提供更多见解。谢谢。
My Question: What actually is happening there? Why didn't the first code update big value.
因为它是 不可变的任意精度整数 意味着它实际上不会更改原始整数,而是在您调用 add
method.Note 时创建一个新整数不可变意味着一旦创建 Object
,它的状态就不能是 changed.For 例如 String
、Integer
、Float
等
Integer i = new Integer(10);//State 1
i = new Integer(20);//State 2 but does not update state 1
big.add(b);
returns 添加后的值,您需要将其存储在其他或相同的变量中。
看看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()
这里说 List.add
实际上会将元素添加到列表中,您可以更改该元素的值,注意 List.add
不会创建新元素,但它实际上会添加原始元素的引用元素.
Why didn't the first code update big value.
BigInteger 是不可变的,您无法更改它,就像您无法更改 String 或任何原始包装器一样。
例如
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()
集合是可变的,但这个标量值不是。
使用可变对象在很大程度上是一种性能让步。如果你有一个每次都需要完整副本的集合,它的性能会很差。
这是方法的 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
这意味着它不是修改值,而是计算一个新值并 returns 它。当您执行 big = big.add(b)
时,您就是 运行 那个方法,获取结果值,并用它替换 big 的原始值。
考虑使用整数、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