转让的左手侧必须是一个变量

转让的左手侧必须是一个变量

问题描述:

为什么不这项工作?

private List<Integer> xShot = new ArrayList<Integer>();
     ...codes
     ...codes
     ...codes
     ...codes
     xShot.get(0) += 5;

不明白为什么一个assignment'isn't的左侧是一个变量。

Can't understand why the left-hand side of an assignment´isn't is a variable..

有人帮忙吗?

如果你只是想通过5来增加和不限于列表&LT;整数GT; 具体而言,你可以说是避免冗长的 xShot.set(0,xShot.get(0)+ 5)键,而是执行此操作:

If you just want to increment by 5 and aren't limited to List<Integer> specifically, you could avoid arguably verbose xShot.set(0, xShot.get(0) + 5) and do this instead:

List<AtomicInteger> xShot = new ArrayList<AtomicInteger>();
xShot.get(0).addAndGet(5);

这将增加的 的AtomicInteger xShot.get(0) 5就地事不宜迟。

This will increment the value of the AtomicInteger in xShot.get(0) by 5 in-place without further ado.