Java, Shifting Elements in an Array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Shifting elements in a Java array means moving elements left or right by a number of positions. Java arrays are fixed-size, so shifting requires overwriting elements and handling the vacated positions. The standard approach uses System.arraycopy() for efficient bulk moves, or a manual loop for full control. For circular shifting (rotation), the three-reverse algorithm provides an O(n) in-place solution.
Left Shift by One Position
Without saving and wrapping the first element, it is simply discarded and the last position is filled with a default or specified value.
Right Shift by One Position
The loop iterates backward to avoid overwriting values before they are moved.
Using System.arraycopy()
System.arraycopy() is a native method that copies array regions efficiently:
System.arraycopy(src, srcPos, dest, destPos, length) handles overlapping regions correctly when source and destination are the same array.
Shifting by K Positions
For shifting (rotating) by k positions, the three-reverse algorithm runs in O(n) time with O(1) space:
Using a Temporary Array
A simpler but O(n) space approach copies elements to their new positions:
Using Collections.rotate()
For List types, Java provides a built-in rotation method:
Note that Collections.rotate() rotates right for positive values, which is the opposite convention of many manual implementations.
Shift Without Rotation (Fill with Default)
If you want to shift without wrapping elements around:
Complexity Comparison
| Approach | Time | Space | In-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 |
Common Pitfalls
- Off-by-one errors in loop direction: When shifting right, iterate backward (
i--). When shifting left, iterate forward (i++). Iterating in the wrong direction overwrites source elements before they are copied. - Forgetting
k % n: Ifkis greater than the array length, you getArrayIndexOutOfBoundsException. Always normalizekwithk = k % arr.lengthfirst. - Empty array or k = 0: Both are edge cases that should return immediately. Dividing by zero (
k % 0) crashes if you do not checkarr.length == 0before normalizing. - Confusing shift with rotation: A shift discards elements that fall off the edge and fills vacated positions with a default. A rotation wraps those elements to the other side. Make sure you implement the correct behavior.
- System.arraycopy overlap:
System.arraycopyhandles overlapping source and destination correctly, but only when they are the same array. Copying between two different arrays with overlapping memory is undefined behavior in theory, though in practice Java arrays do not share memory.
Summary
- Use a manual loop for single-position shifts with full control
- Use
System.arraycopy()for efficient bulk element moves - Use the three-reverse algorithm for O(n) time, O(1) space rotation by k positions
- Use
Collections.rotate()forListtypes (positive = right, negative = left) - Always normalize
kwith modulo and handle empty array edge cases
Related reading
- Java Sorting an array based on another array with indexOf method
- Java Stream API - Best way to transform a list map or forEach?
- Java String array is there a size of method?
- Java time-based map/cache with expiring keys
- Java SimpleDateFormatyyyy-MM-dd'T'HHmmss'Z' gives timezone as IST
- Java Singleton and Synchronization
- Java using much more memory than heap size or size correctly Docker memory limit
- Java using much more memory than heap size or size correctly Docker memory limit

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.