赋值的左侧必须是变量

发布于 2024-11-30 20:10:23 字数 231 浏览 0 评论 0原文

为什么这不起作用?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鸵鸟症 2024-12-07 20:10:23

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

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

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

例如:

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

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

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

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.set(0, xShot.get(0) + 5);
北方的巷 2024-12-07 20:10:23

xShot.get(0) 是一个返回值的方法调用。变量是用保存值的​​类型声明的内容,例如 int x;、String name; 或 List。 xShot 来自您的示例。这些是 Java 中唯一可以使用 赋值来赋值的东西运算符

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> xShot from your example. Those are the only things in Java that you can assign a value to using an assignment operator.

_蜘蛛 2024-12-07 20:10:23

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

此外,Integer 是不可变的(您无法更改其值),因此您必须替换位置 0 处的对象为 < em>new Integer 具有计算值。

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

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

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 position 0 with a new Integer that has the calculated value.

You can achieve the intention of that line like this:

xShot.set(0, xShot.get(0) + 5);
南冥有猫 2024-12-07 20:10:23

这就像 Java 中的说法:

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

左侧 (5) 不是可变的。

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

因此,执行您想要的操作的正确方法是:

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

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

It is like saying in Java:

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

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:

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

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

随风而去 2024-12-07 20:10:23

如果您只想增加 5 并且不限于 List 具体,您可以避免冗长的 xShot.set(0, xShot.get(0) + 5) 并执行此操作:

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

这将增加 AtomicIntegerxShot.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);

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

短叹 2024-12-07 20:10:23

赋值的左侧必须显式是一个变量,因为在您的语句情况下,表达式也可以是常量,如果允许,这将是一个错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文