Java – assignment of value in array index
•
Java
Please check the following code snippet to let me know how to output 1.2
int[] a = { 1,2,3,4 }; int[] b = { 2,1,0 }; System.out.println( a [ (a = b)[3] ] ); System.out.println(a[0]);
Actual answer 1 2
thank you
Solution
I'll try to explain:
A [(a = b) [3]] will be executed in the following order:
>A [...] – read array A and store reference > (a = b) for it – set variable a to reference array B > (a = b) [3] – read the fourth element of array B (due to step 2), the value is 0 > a [(a = b) [3]] – now equal to a [0] (due to steps 1 and 3) and the value is 1
A [0] now produces 2 because it references array B (because of step 2) and the first element in the array is 2
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码