Java
Array Manipulation
Element Shifting
Programming
Data Structures

How to shift elements in an array in Java

Codemia Knowledge Hub ·

Java arrays are fixed-size, so shifting means overwriting elements and deciding what goes into the vacated positions. Use System.arraycopy() for efficient bulk moves or a manual loop for full control. For a circular shift (rotation) by k positions, the three-reverse algorithm runs in O(n) time and in place.

Left shift by one position

java
1int[] arr = {1, 2, 3, 4, 5};
2
3// Save the first element
4int first = arr[0];
5
6// Shift all elements one position to the left
7for (int i = 0; i < arr.length - 1; i++) {
8    arr[i] = arr[i + 1];
9}
10
11// Place the saved element at the end (rotation)
12arr[arr.length - 1] = first;
13
14// Result: [2, 3, 4, 5, 1]

If you do not save and wrap the first element, it is discarded and the last position gets a default or chosen value instead.

Right shift by one position

java
1int[] arr = {1, 2, 3, 4, 5};
2
3int last = arr[arr.length - 1];
4
5// Shift all elements one position to the right
6for (int i = arr.length - 1; i > 0; i--) {
7    arr[i] = arr[i - 1];
8}
9
10arr[0] = last;
11
12// Result: [5, 1, 2, 3, 4]

The loop runs backward so values are not overwritten before they move.

Shifting with System.arraycopy()

System.arraycopy() is a native method that copies array regions efficiently:

java
1int[] arr = {1, 2, 3, 4, 5};
2
3// Left shift by 1: copy arr[1..4] to arr[0..3]
4int first = arr[0];
5System.arraycopy(arr, 1, arr, 0, arr.length - 1);
6arr[arr.length - 1] = first;
7// Result: [2, 3, 4, 5, 1]
8
9// Right shift by 1: copy arr[0..3] to arr[1..4]
10int[] arr2 = {1, 2, 3, 4, 5};
11int last = arr2[arr2.length - 1];
12System.arraycopy(arr2, 0, arr2, 1, arr2.length - 1);
13arr2[0] = last;
14// Result: [5, 1, 2, 3, 4]

System.arraycopy(src, srcPos, dest, destPos, length) handles overlapping regions correctly when source and destination are the same array.

Rotating by k positions with three reversals

The three-reverse algorithm rotates by k positions in O(n) time with O(1) space:

java
1public static void rotateLeft(int[] arr, int k) {
2    int n = arr.length;
3    k = k % n;  // Handle k > n
4    if (k == 0) return;
5
6    reverse(arr, 0, k - 1);
7    reverse(arr, k, n - 1);
8    reverse(arr, 0, n - 1);
9}
10
11public static void rotateRight(int[] arr, int k) {
12    int n = arr.length;
13    k = k % n;
14    if (k == 0) return;
15
16    reverse(arr, 0, n - 1);
17    reverse(arr, 0, k - 1);
18    reverse(arr, k, n - 1);
19}
20
21private static void reverse(int[] arr, int start, int end) {
22    while (start < end) {
23        int temp = arr[start];
24        arr[start] = arr[end];
25        arr[end] = temp;
26        start++;
27        end--;
28    }
29}
30
31// Usage
32int[] arr = {1, 2, 3, 4, 5};
33rotateLeft(arr, 2);
34// Result: [3, 4, 5, 1, 2]

Rotating into a temporary array

A simpler approach that uses O(n) extra space copies each element to its new position:

java
1public static int[] shiftLeft(int[] arr, int k) {
2    int n = arr.length;
3    k = k % n;
4    int[] result = new int[n];
5
6    for (int i = 0; i < n; i++) {
7        result[i] = arr[(i + k) % n];
8    }
9
10    return result;
11}
12
13int[] arr = {1, 2, 3, 4, 5};
14int[] shifted = shiftLeft(arr, 2);
15// shifted: [3, 4, 5, 1, 2]

Collections.rotate() for lists

For List types, Java has a built-in rotation:

java
1import java.util.*;
2
3List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
4
5// Rotate right by 2 (positive = right)
6Collections.rotate(list, 2);
7// Result: [4, 5, 1, 2, 3]
8
9// Rotate left by 2 (negative = left)
10Collections.rotate(list, -2);
11// Result: [1, 2, 3, 4, 5]  (back to original)

Collections.rotate() rotates right for positive distances, the opposite of many manual implementations.

Shifting without rotation (fill with a default)

To shift without wrapping elements around:

java
1int[] arr = {1, 2, 3, 4, 5};
2
3// Left shift by 2, fill with 0
4System.arraycopy(arr, 2, arr, 0, arr.length - 2);
5arr[arr.length - 2] = 0;
6arr[arr.length - 1] = 0;
7// Result: [3, 4, 5, 0, 0]
8
9// Right shift by 2, fill with 0
10int[] arr2 = {1, 2, 3, 4, 5};
11System.arraycopy(arr2, 0, arr2, 2, arr2.length - 2);
12arr2[0] = 0;
13arr2[1] = 0;
14// Result: [0, 0, 1, 2, 3]

Complexity of each approach

ApproachTimeSpaceIn-Place
Manual loop (shift by 1)O(n)O(1)Yes
System.arraycopy (shift by 1)O(n)O(1)Yes
Three-reverse (shift by k)O(n)O(1)Yes
Temporary array (shift by k)O(n)O(n)No
Collections.rotate (List)O(n)O(1)Yes

Bugs to watch for

  • Wrong loop direction: iterate backward (i--) for a right shift and forward (i++) for a left shift. The wrong direction overwrites source elements before they are copied.
  • Forgetting k % n: if k is larger than the array length you get ArrayIndexOutOfBoundsException. Normalize with k = k % arr.length first.
  • Empty arrays and k = 0: return immediately. k % 0 throws if you do not check arr.length == 0 before normalizing.
  • Shift versus rotation: a shift drops elements that fall off the edge and fills the gap with a default; a rotation wraps them to the other side. Implement the one you actually need.
  • Overlap in System.arraycopy: overlapping source and destination ranges are handled correctly within the same array. Two different Java arrays never share memory, so overlap is not a concern between them.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms