Hi, here is the code pasted using the code tags
Code: Select all
public class TestValue{
public static void main(String[] args) {
Value[] arr = new Value[4];
for(int i = 0; i<arr.length; i++) {
arr[i] = new Value(i);
}
Value t = arr[0];
arr[0] = arr[3];
arr[3] = t;
t.setI(arr[0].getI());
for(int i = 0; i<arr.length; i++) {
System.out.print(arr[i].getI()+ " ");
}
}
}
public class Value{
private int _i;
public Value(int i){
_i = i;
}
public int getI(){
return _i;
}
public void setI (int i){
_i = i;
}
}
What this code does is the following:
a) Value[] arr = new Value[4]; initializes an empty array objects of type Value of size 4
b) The first for iterates from numbers from 0 to 4 and associate to the index of the arrays the following objects:
arr[0] = Value(0)
arr[1] = Value(1)
arr[2] = Value(2)
arr[3] = Value(3)
So at this point you will have in your array: 0 1 2 3
c) The statement: Value t = arr[0]; will assign to the variable t the value of arr[0] that is Value(0)
d) The statement: arr[0] = arr[3]; will assign to arr[0] the value of arr[3] that is: Value(3)
So at this point you will have in your array: 3 1 2 3
e) The statement: arr[3] = t; will assign to arr[3] the value of arr[0]
So, at this point you will have in your array 3 1 2 3
f) The statement: t.setI(arr[0].getI()); will assing to t the value of arr[0]
So, at this point you will have in your array 3 1 2 3
g) The last for iterates the array and print each value