java 赋值的左侧必须是变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7141461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:49:21  来源:igfitidea点击:

The left-hand side of an assignment must be a variable

javalistarraylistinteger

提问by Daniel

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?

有人帮忙吗?

采纳答案by Philipp Reichart

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:

如果你只想增加 5 并且不限于List<Integer>特定的,你可以避免冗长的说法xShot.set(0, xShot.get(0) + 5),而是这样做:

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

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

这将使AtomicIntegerin的值xShot.get(0)就地增加 5,无需多说。

回答by Ryan Stewart

xShot.get(0)is a method call that returns a value. A variable is something you declare with a type that holds a value, like int x;, String name;, or List<Integer> xShotfrom your example. Those are the only things in Java that you can assign a value to using an assignment operator.

xShot.get(0)是一个返回值的方法调用。变量是你保存的值,喜欢的类型声明的东西int x;String name;或者List<Integer> xShot从你的榜样。这些是 Java 中唯一可以使用赋值运算符赋值的东西

回答by Ryan Amos

Although xShot.get(0) is a number, it is not a variable. You need to provide a variable for this to work. That said

尽管 xShot.get(0) 是一个数字,但它不是一个变量。您需要提供一个变量才能使其工作。那说

int i = xShot.get(0);
i += 5;

Will not work. iwill 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.

不管用。i将增加 5,但 xShot 在位置 5 的对象不是同一个对象。您需要获取、修改和设置变量。

For example:

例如:

xShot.set(0, xShot.get(0) + 5);

回答by Bohemian

xShot.get(0)returnsan object; it isn't a variable, so you can't assign to it.

xShot.get(0)返回一个对象;它不是一个变量,所以你不能分配给它。

Also, Integeris immutable (you can't change its value), so you would have to replacethe object at position 0with a newIntegerthat has the calculated value.

此外,Integer是不可变的(您不能更改其值),因此您必须用具有计算值的对象替换位置处的对象。0Integer

You can achieve the intention of that line like this:

您可以像这样实现该行的意图:

xShot.set(0, xShot.get(0) + 5);

回答by Martijn Courteaux

It is like saying in Java:

这就像在 Java 中说的:

5 = 6; // "Assign 5 to 6"

The left side (5) isn't variable.

左侧 ( 5) 不是可变的。

Why is this example statement relevant? Because of Java uses always"pass by value". Which means that the return valueof 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.

为什么这个示例语句是相关的?因为 Java 使用总是“按值传递”。这意味着方法的返回也是“按值返回”。这是纯粹的数学:你不能改变一个值,你可以改变一个变量。对于 Java 也是如此。五永远不可能变成六。
换句话说:只能为变量赋值。

So, the correct way of doing what you want is:

所以,做你想做的正确方法是:

xShot.set(0, xShot.get(0) + 5);

Edit:In your situation: xShot.get(int)doesn't return a variable, but a value.

编辑:在您的情况下:xShot.get(int)不返回变量,而是返回值。

回答by Bhaskar

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

赋值的左侧必须明确是一个变量,因为在你的语句中,表达式也可以是一个常量,如果允许,这将是一个错误