本文介绍了赋值的左侧必须是变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么这不起作用?
private List<Integer> xShot = new ArrayList<Integer>();
...codes
...codes
...codes
...codes
xShot.get(0) += 5;
无法理解为什么赋值的左侧不是变量..
Can't understand why the left-hand side of an assignment´isn't is a variable..
有人帮忙吗?
推荐答案
如果你只是想增加 5 并且不限于 List
,你可以避免有争议的冗长 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.
这篇关于赋值的左侧必须是变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!