赋值的左侧必须是变量
为什么这不起作用?
private List<Integer> xShot = new ArrayList<Integer>();
...codes
...codes
...codes
...codes
xShot.get(0) += 5;
无法理解为什么赋值的左侧不是变量。
有人帮忙吗?
Why doesn't this work?
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..
Someone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然 xShot.get(0) 是一个数字,但它不是一个变量。您需要提供一个变量才能使其工作。那说
不会工作。
i
将增加 5,但位置 5 中 xShot 的对象不是同一个对象。您需要获取、修改和设置变量。例如:
Although xShot.get(0) is a number, it is not a variable. You need to provide a variable for this to work. That said
Will not work.
i
will be incremented by 5, but xShot's object in location 5 is not the same object. You need to get, modify, and set the variable.For example:
xShot.get(0)
是一个返回值的方法调用。变量是用保存值的类型声明的内容,例如 int x;、String name; 或 ListxShot.get(0)
is a method call that returns a value. A variable is something you declare with a type that holds a value, likeint x;
,String name;
, orList<Integer> xShot
from your example. Those are the only things in Java that you can assign a value to using an assignment operator.xShot.get(0)
返回一个对象;它不是一个变量,所以你不能给它赋值。此外,
Integer
是不可变的(您无法更改其值),因此您必须替换位置0
处的对象为 < em>newInteger
具有计算值。您可以像这样实现该行的意图:
xShot.get(0)
returns an object; it isn't a variable, so you can't assign to it.Also,
Integer
is immutable (you can't change its value), so you would have to replace the object at position0
with a newInteger
that has the calculated value.You can achieve the intention of that line like this:
这就像 Java 中的说法:
左侧 (
5
) 不是可变的。为什么这个示例语句相关?因为Java使用总是“按值传递”。这意味着方法的返回值也是“按值返回”。
这是纯粹的数学:你不能改变一个值,你可以改变一个变量。对于 Java 来说也是如此。五永远不可能变成六。
换句话说:只能将一个值分配给变量。
因此,执行您想要的操作的正确方法是:
编辑:在您的情况下:
xShot .get(int)
不返回变量,而是返回值。It is like saying in Java:
The left side (
5
) isn't variable.Why is this example statement relevant? Because of Java uses always "pass by value". Which means that the return value of a method is also "return by value".
This is pure mathematical: you can't change a value, you can change a variable. The same for Java. Five can never become six.
In other words: Only a value can be assigned to a variable.
So, the correct way of doing what you want is:
Edit: In your situation:
xShot.get(int)
doesn't return a variable, but a value.如果您只想增加 5 并且不限于
List
具体,您可以避免冗长的xShot.set(0, xShot.get(0) + 5)
并执行此操作:这将增加
AtomicInteger
在xShot.get(0)
中原地增加 5,无需多言。If you just want to increment by 5 and aren't limited to
List<Integer>
specifically, you could avoid arguably verbosexShot.set(0, xShot.get(0) + 5)
and do this instead:This will increment the value of the
AtomicInteger
inxShot.get(0)
by 5 in-place without further ado.赋值的左侧必须显式是一个变量,因为在您的语句情况下,表达式也可以是常量,如果允许,这将是一个错误
The left-hand side of the assignment has to be explicitly a variable because in your case of statement , the expression could be a constant also , which would be a error if allowed