Java Program to shift array elements to the right


Let us first create an int array −

int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };

Now, shift array elements to the right with arraycopy() and placing the elements correctly so that it gets shifted to the right −

System.arraycopy(arr, 0, arr, 1, arr.length - 1);

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
      System.out.println("Initial array...
"+Arrays.toString(arr));       System.arraycopy(arr, 0, arr, 1, arr.length - 1);       System.out.println("Array after shifting to the right...");       System.out.println(Arrays.toString(arr));    } }

Output

Initial array...
[10, 20, 30, 40, 50, 60, 70, 80, 90]
Array after shifting to the right...
[10, 10, 20, 30, 40, 50, 60, 70, 80]

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

299 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements