Java 数组 - 为什么输出是“1”? ?

发布于 2024-12-19 13:44:03 字数 404 浏览 2 评论 0原文

为什么本例中的输出是1

public static void main(String[] args){
 int[] a = { 1, 2, 3, 4 };   
 int[] b = { 2, 3, 1, 0 };   
 System.out.println( a [ (a = b)[3] ] );   
}

我认为会是2。即,表达式的计算结果为:

a[(a=b)[3]]
a[b[3]]    //because a is now pointing to b
a[0]   

因为 a 指向 b,所以 a[0] 不应该是 2 吗?

提前致谢。

Why is the output in this example 1?

public static void main(String[] args){
 int[] a = { 1, 2, 3, 4 };   
 int[] b = { 2, 3, 1, 0 };   
 System.out.println( a [ (a = b)[3] ] );   
}

I thought it would be 2. i.e., the expression is evaluated as:

a[(a=b)[3]]
a[b[3]]    //because a is now pointing to b
a[0]   

Shouldn't a[0] be 2 because a is pointing to b?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

苏佲洛 2024-12-26 13:44:03

每个运算符的参数都是从左到右计算的。即,[...] 前面的 a 在其内容之前被评估,此时它仍然引用第一个数组。

The arguments to each operator are evaluated left-to-right. I.e., the a in front of the [...] is evaluated before its contents, at which point it still refers to the first array.

友谊不毕业 2024-12-26 13:44:03

这也让我很奇怪......但是,请检查第 15.7.1 节 此处

本质上,操作数是从左到右计算的。但还要注意这一点:

建议代码不要严重依赖此规范。当每个表达式最多包含一个副作用(作为其最外层操作),并且代码不完全依赖于表达式从左到右求值所导致的异常时,代码通常会更清晰。

That weirded me out as well... however, check section 15.7.1 over here

Essentially, operands are evaluated from left to right. But also note this:

It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

也只是曾经 2024-12-26 13:44:03
a [ (a = b)[3] ] )

解释如下:

a = b => a = {2, 3, 1, 0};
( a = b )[3] => 0;

这里有一个技巧:a 被评估为 b 分配给它之前的值。

a[(a = b)[3]) => a[0] = 1;

考虑一下 Java 中的运算符优先级。应该更明显一点。

a [ (a = b)[3] ] )

is interpreted as follows:

a = b => a = {2, 3, 1, 0};
( a = b )[3] => 0;

Here is the trick here: a is evaluated as the value before b is assigned to it.

a[(a = b)[3]) => a[0] = 1;

Think about the operator precedence in Java. It should be a bit more obvious.

深爱不及久伴 2024-12-26 13:44:03

正如 Marcelo Cantos 先生所指出的,每个运算符的参数都是从左到右评估的。因此,我认为执行如下:

a[(a=b)[3]]

外部“a”将获取“1,2,3,4”,然后评估其参数 (a=b)[3]。因此现在 a=b 并且返回 b 数组中索引 3 处的元素,该元素也由 a 指向。

因此,我们从参数评估中得到“0”。如前所述,外部 a 仍然引用旧内容,因此给出 1,2,3,4 数组中的 a[0]。

因此我们得到“1”。

这是我的理解。如果有误请告诉我。

谢谢,

As Mr Marcelo Cantos Pointed out, Arguments to each operator are evaluated from left to right. Therefore here is what I think the execution is

a[(a=b)[3]]

Here the outer 'a' will fetch "1,2,3,4" and then its argument (a=b)[3] is evaluated. Thus now a=b and the element at index 3 in b array is returned which is also pointed by a.

Hence we get a '0' from the argument evaluation. As said previously, outer a still refers to old contents Thus gives a[0] in 1,2,3,4 array.

Therefore we get a '1'.

This is my understanding. Please let me know if its wrong.

Thanks,

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