To shift elements in an array in Java, use the
System.arraycopy
function.Shift left
Here is how you can shift your array to the left:
int[] array = new int[] { 3, 5, 1, 9, 10 };
System.arraycopy(array, 1, array, 0, array.length - 1);
System.out.println(Arrays.toString(array)); // [5, 1, 9, 10, 10]
Shift right
Here is how you can shift your array to the right:
int[] array = new int[] { 3, 5, 1, 9, 10 };
System.arraycopy(array, 0, array, 1, array.length - 1);
System.out.println(Arrays.toString(array)); // [3, 3, 5, 1, 9]