Hi guys,
I solved this task, but I don't like that I had to create a new int[] nums that needed to be populated with the reversed integers from the originating array, and I was wondering if there is another, better way to solve it without being necessary creating a new temporary int[] ?
Thank you in advance!
public class Solution {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
printArray(array);
reverseArray(array);
printArray(array);
}
public static void reverseArray(int[] array) {
int[] nums = new int[array.length];
int i, j;
for (i = 0, j = array.length-1; i < nums.length; i++, j--) {
nums[i] = array[j];
}
for (i = 0, j = 0; i < nums.length; i++, j++) {
array[i] = nums[j];
}
}
public static void printArray(int[] array) {
for (int i : array) {
System.out.print(i + ", ");
}
System.out.println();
}
}