0
Can someone explain how this segment pf code works? Thank you!
import java.util.Scanner; public class Main{ public static void main(String args[]){ int [][] arr = {{5,2,6},{1,3,8},{7,0,4}}; int y = arr[1][0]; int x = arr[2][1]; int z = arr[--y][++x]; System.out.println(z); } }
2 Antworten
+ 2
Arr is a multidimensional array, it means that have an array/s into another array
after the arr declaration arr values are that:
arr[0][0]=5
arr[0][1]=2
arr[0][2]=6
arr[1][0]=1
arr[1][1]=3
arr[1][2]=8
. . . so on.
-- and ++ operators before a variable, increments or decrement the value before an operation, it means that if you do:
int x =5;
System.out.println(++x + 5);
The result will be 6 + 5: 11.
Because you increment x before the sum operation.
So let's solve the problem:
int y = arr[1][0] = 1
because arr[1][0] value is 1, I explained it at the beggining
int x = arr[2][1] = 0
int z = arr[--y][++x] = 2
because you are decrementing y and incrementing x before access the array index
So if y value is 1 and you do --y it comes 0
the same with x, if x value is 0 and you do ++x it comes 1
then
int z = arr[0][1] = 2
Hope it helps you!!
+ 1
explained it like a true genius !!