static int[] fun1(int[] ar){
int[] auxarray = new int[ar.length];
int j = ar.length;
for (int i = 0; i < ar.length; i++) {
auxarray[j - 1] = ar[i];
j = j - 1;
}
return ar;
}
I have tried to implement a swap method to modify the same array, but it didn't work (tested with a void method and printed the result inside it: same as the initial array)
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.print("Please enter the size of the array: ");
size = input.nextInt();
array = new int[size]; //array and size are declared private static globally
for(int i = 0; i<size; i++){
array[i] = input.nextInt();
}
System.out.println("Your reversed string is:");
int[] reversedarray = fun1(array);
for(int i = 0; i < size; i++){
System.out.print(reversedarray[i] + ' ');
}
}
This returns 3334353637.. in all cases. Any solution or any idea on what I have done wrong?