I have to reverse two arrays so that, they both have the same values but different references.
Here is my code so far.
But how to achieve that when both arrays are pointing to the same program arguments?
And why does String[]
reference reverse the String[]
values instead of reversing the program arguments?
For example. If the program arguments were 1 2 3 4 5
:
String[] values = 5 4 3 2 1
String[] reference = 1 2 3 4 5
public static void main(String[] args) {
String[] values = changeValues(args);
System.out.println(Arrays.toString(values));
String[] reference = changeReference(args);
System.out.println(Arrays.toString(reference));
if (!testSameValues(values, reference)) {
System.out.println("Error: Values do not match !");
}
if (testSameReference(values, reference)) {
System.out.println("Error: References are the same !");
}
}
public static String[] changeValues(String[] x) {
for (int i = 0; i < x.length / 2; i++) {
String temp = x[i];
x[i] = x[(x.length - 1) - i];
x[(x.length - 1) - i] = temp;
}
return x;
}
public static String[] changeReference(String[] y) {
for (int i = 0; i < y.length / 2; i++) {
String temp = y[i];
y[i] = y[(y.length - 1) - i];
y[(y.length - 1) - i] = temp;
}
return y;
}
public static boolean testSameValues(String[] x, String[] y) {
if (x.equals(y)) {
return true;
} else
return false;
}
public static boolean testSameReference(String[] x, String[] y) {
if (x == y) {
return true;
} else
return false;
}