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.
Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Array Initialization in Java To use the array, we can initialize it with the new keyword, followed by the data type of our array, and rectangular brackets containing its size: int[] intArray = new int[10]; This allocates the memory for an array of size 10 . This size is immutable.
Use List. clear() method to empty an array.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With