- You weren't populating the rotated array. You were modifying the array reference!
- You were checking if index == 4 when you could do array.length - 1.
- You need to determine the target index as being (index + rotationTimes) then if it exceeds array.length - 1 (the last index) you use the modulus operator to make sure it doesnt overflow. For example, if the current index is 4 (the last one) and the iterations is 1, (4 + 1) is greater than 4 so the target index is (4 + 1) % 5 = 0.
public static void main(String[] args) {
int[] array = new int[] {1, 2, 3, 4, 5};
int[] rotated = new int [array.length];
System.out.println("Enter amount of times to rotate: ");
Scanner scan = new Scanner(System.in);
int rotationTimes = scan.nextInt();
if (rotationTimes > 4){
rotationTimes %= 5;
}
for (int index = 0; index < array.length; index++) {
int targetIndex = index + rotationTimes;
if (targetIndex > array.length - 1) {
targetIndex %= array.length;
}
rotated[targetIndex] = array[index];
}
System.out.println("Input: " + Arrays.toString(array));
System.out.println("Rotations: " + rotationTimes);
System.out.println("Output: " + Arrays.toString(rotated));
}
Input: [1, 2, 3, 4, 5]
Rotations: 1
Output: [5, 1, 2, 3, 4]
Input: [1, 2, 3, 4, 5]
Rotations: 2
Output: [4, 5, 1, 2, 3]