在数组索引中赋值

时间:2021-11-10 05:33:03

Please look at the below code snippet and let me know how the out comes out as 1 2 .

请查看下面的代码片段,让我知道如何以1 2的形式出现。

int[] a = { 1, 2, 3, 4 };
int[] b = { 2, 3, 1, 0 };
System.out.println( a [ (a = b)[3] ] );
System.out.println(a[0]);

Actual answer 1 2

实际答案1 2

Thanks

3 个解决方案

#1


6  

I'll try to explain:

我会试着解释一下:

a [ (a = b)[3] ] will be executed in the following order:

a [(a = b)[3]]将按以下顺序执行:

  1. a [...] - the array a will be read and the reference is stored for that
  2. a [...] - 将读取数组a并为其存储引用

  3. (a = b) - the variable a is set to reference array b
  4. (a = b) - 变量a设置为参考数组b

  5. (a=b)[3] - the 4th element of array b is read (because of step 2) , the value is 0
  6. (a = b)[3] - 读取数组b的第4个元素(由于步骤2),该值为0

  7. a [ (a = b)[3] ] - this is now equal to a[0] (because of steps 1 and 3), the value is 1
  8. a [(a = b)[3]] - 现在等于a [0](因为步骤1和3),值为1

a[0] now yields 2 since a references array b (because of step 2) and the first element in that array is 2.

a [0]现在产生2,因为引用数组b(因为步骤2)并且该数组中的第一个元素是2。

#2


7  

Seriously, what is the purpose of this? Why would you ever wanna do something that makes the code so unreadable. What would you expect the outcome to be?

说真的,这是什么目的?你为什么要做一些使代码难以理解的事情。您期望结果如何?

The result of System.out.println( a [ (a = b)[3] ] ); has to do with the order in which things are pushed to the evaluation stack ... e.g.

System.out.println的结果(a [(a = b)[3]]);与事物被推到评估堆栈的顺序有关...例如

  1. reference to a
  2. 参考a

  3. change reference stored in a to that stored in b
  4. 将存储在a中的引用更改为存储在b中的引用

  5. evaluated b[3] => 0
  6. 评估b [3] => 0

  7. print index 0 of the array to which reference was pushed in 1.), i.e. the original a
  8. 在1。中推送引用的数组的打印索引0,即原始a

so it prints the element at 0 of the original a array

所以它将元素打印在原始数组的0处

System.out.println(a[0]); is then simply b[0]

的System.out.println(A [0]);那么就是b [0]

#3


0  

First two lines initialize your arrays. First sysout assigns b to a then prints a[3], ie. your a is now having values {2,3,1,0}. Second sysout prints a[0].

前两行初始化您的数组。第一个sysout将b分配给a然后打印[3],即。你的a现在有值{2,3,1,0}。第二个sysout打印一个[0]。

#1


6  

I'll try to explain:

我会试着解释一下:

a [ (a = b)[3] ] will be executed in the following order:

a [(a = b)[3]]将按以下顺序执行:

  1. a [...] - the array a will be read and the reference is stored for that
  2. a [...] - 将读取数组a并为其存储引用

  3. (a = b) - the variable a is set to reference array b
  4. (a = b) - 变量a设置为参考数组b

  5. (a=b)[3] - the 4th element of array b is read (because of step 2) , the value is 0
  6. (a = b)[3] - 读取数组b的第4个元素(由于步骤2),该值为0

  7. a [ (a = b)[3] ] - this is now equal to a[0] (because of steps 1 and 3), the value is 1
  8. a [(a = b)[3]] - 现在等于a [0](因为步骤1和3),值为1

a[0] now yields 2 since a references array b (because of step 2) and the first element in that array is 2.

a [0]现在产生2,因为引用数组b(因为步骤2)并且该数组中的第一个元素是2。

#2


7  

Seriously, what is the purpose of this? Why would you ever wanna do something that makes the code so unreadable. What would you expect the outcome to be?

说真的,这是什么目的?你为什么要做一些使代码难以理解的事情。您期望结果如何?

The result of System.out.println( a [ (a = b)[3] ] ); has to do with the order in which things are pushed to the evaluation stack ... e.g.

System.out.println的结果(a [(a = b)[3]]);与事物被推到评估堆栈的顺序有关...例如

  1. reference to a
  2. 参考a

  3. change reference stored in a to that stored in b
  4. 将存储在a中的引用更改为存储在b中的引用

  5. evaluated b[3] => 0
  6. 评估b [3] => 0

  7. print index 0 of the array to which reference was pushed in 1.), i.e. the original a
  8. 在1。中推送引用的数组的打印索引0,即原始a

so it prints the element at 0 of the original a array

所以它将元素打印在原始数组的0处

System.out.println(a[0]); is then simply b[0]

的System.out.println(A [0]);那么就是b [0]

#3


0  

First two lines initialize your arrays. First sysout assigns b to a then prints a[3], ie. your a is now having values {2,3,1,0}. Second sysout prints a[0].

前两行初始化您的数组。第一个sysout将b分配给a然后打印[3],即。你的a现在有值{2,3,1,0}。第二个sysout打印一个[0]。