public class Foo {
public void crazyArray(int[] vals){
int[] newVals = { 1, 7, 6, 1, 3 };
vals[0] -= vals[0] - newVals[3];
vals[3] = vals[2] + vals[1] - newVals[4];
vals[2] -= vals[0] *2;
vals = newVals;
vals[1] -= vals[2];
newVals[2] *= 3 + vals[3];
vals[1] -= 14 + vals[2];
}
public static void main(String[] args) {
Foo foo = new Foo();
int[] values = {0 ,1 ,3, 1};
foo.crazyArray(values);
for (int n:values){
System.out.printf("%4d\n", values[0] - values[n]);
}
}
}
If you have any doubts,please give me comment...
Crazy array will do the process like this:
vals = {0, 1, 3, 1}
newVals = { 1, 7, 6, 1, 3 };
vals[0] = 0 - 0 - 1= 1
vals[3] = 3 + 1 - 3= 1
vals[2] = 3 - 1 * 2= 1
vals = [1, 7, 6, 1, 3]
vals[1] = 7 - 6= 1
newVals[2] = 6 * 3 + 1= 24
vals[1] = 1 - 14 + 24= -37
Finally Values array in function: [1, -37, 24, 1, 3]
But some of index only changed in main, the values array after calling Crazy array is: [1, 1, 1, 1], because newVals array not created dynamically, it is static. So it will not permit the change values by reference.
values array in main after calling crazyArray function: [1, 1, 1, 1]
1-1 = 0
1-1 = 0
1-1 = 0
1-1 = 0
Get Answers For Free
Most questions answered within 1 hours.