[英]How can I increment BigDecimal in java through for loop?
How can I increment BigDecimal in java through for loop? 如何通过for循环在Java中递增BigDecimal? This is the codes that I am currently running and I don't understand why it won't increment the BigDecimal instances
这是我当前正在运行的代码,但我不明白为什么它不会增加BigDecimal实例
package app;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Test {
private List<BigDecimal> ticketQuantity = new ArrayList<>();
List<String> someNumber = new ArrayList<>();
public Test() {
ticketQuantity.add(new BigDecimal(0));
ticketQuantity.add(new BigDecimal(0));
ticketQuantity.add(new BigDecimal(0));
ticketQuantity.add(new BigDecimal(0));
someNumber.add("10");
someNumber.add("10");
someNumber.add("10");
someNumber.add("10");
System.out.println(ticketQuantity);
int i = 0;
for (BigDecimal x : ticketQuantity) {
x.add(new BigDecimal(someNumber.get(i)));
i++;
}
System.out.println(ticketQuantity);
}
public static void main(String[] args) {
new Test();
}
}
What did I miss? 我错过了什么? I am hoping that
someNumber would equals to [10, 10, 10, 10]
but then [0, 0, 0, 0]
is printed :( 我希望
someNumber would equals to [10, 10, 10, 10]
,然后打印[0,0,0,0 [0, 0, 0, 0]
:(
BigDecimal
represents: BigDecimal
表示:
Immutable, arbitrary-precision signed decimal numbers
不可变,任意精度的带符号十进制数字
The immutability means you can't modify an instance. 不变性意味着您无法修改实例。 The
add
method will return a new BigDecimal
. add
方法将返回一个新的 BigDecimal
。 If you want to change the value in the list, you will have to go through the loop by index and call List.set
with the index to replace and the result of BigDecimal.add
: 如果要更改列表中的值,则必须按索引循环,并使用要替换的索引和
BigDecimal.add
的结果调用List.set
:
for (int i = 0; i < ticketQuantity.size(); i++) {
ticketQuantity.set(i, ticketQuantity.get(i).add(someNumber.get(i)));
}
As explained in other answer, add()
method will return a new BigDecimal
and wouldn't modify the original instance due to BigDecimal
being immutable so you need to do as below, 如其他答案所述,
add()
方法将返回一个新的BigDecimal
并且由于BigDecimal
是不可变的,因此不会修改原始实例,因此您需要执行以下操作,
int i = 0;
for (BigDecimal x : ticketQuantity) {
x = x.add(new BigDecimal(someNumber.get(i)));
ticketQuantity.set(i, x);
i++;
}
As others have stated BigDecimal is an immutable class. 正如其他人所说,BigDecimal是一个不可变的类。 The method
BigDecimal#add
actually returns another instance of BigDecimal
, doesn't update the operand BigDecimal
object. 方法
BigDecimal#add
实际上会返回BigDecimal
另一个实例,而不更新操作数BigDecimal
对象。 To achieve your object, you can try the following way: 要实现您的目标,可以尝试以下方法:
public class Test {
private List<BigDecimal> ticketQuantity = new ArrayList<>();
List<String> someNumber = new ArrayList<>();
public Test() {
someNumber.add("10");
someNumber.add("10");
someNumber.add("10");
someNumber.add("10");
System.out.println(ticketQuantity);
for (String x : someNumber) {
ticketQuantity.add(new BigDecimal(x));
}
System.out.println(ticketQuantity);
}
public static void main(String[] args) {
new Test();
}
}
add() method adds new elements at the end of list. add()方法将新元素添加到列表的末尾。 I think you are trying to overwrite on first four values you 've added previously.
我认为您正在尝试覆盖之前添加的前四个值。 so use set(int index,E element);
所以使用set(int index,E element);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.