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
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
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:
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:
Rotating into a temporary array
A simpler approach that uses O(n) extra space copies each element to its new position:
Collections.rotate() for lists
For List types, Java has a built-in rotation:
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:
Complexity of each approach
| 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 |
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: ifkis larger than the array length you getArrayIndexOutOfBoundsException. Normalize withk = k % arr.lengthfirst. - Empty arrays and k = 0: return immediately.
k % 0throws if you do not checkarr.length == 0before 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.
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.lang.OutOfMemoryError Java heap space
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

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 courseData 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.