Shift an Array in Java

Rupam Yadav Jan 30, 2023 May 14, 2021
  1. Use the for Loop and a temp Variable to Shift an Array in Java
  2. Use the skip() Method to Shift an Array in Java 8
  3. Use the Collections.rotate(List<type> list, int distance) to Shift an Array in Java
Shift an Array in Java

In this article, we will go through the ways to shift an element of an array in Java.

Use the for Loop and a temp Variable to Shift an Array in Java

In this code, we have an array variable that is a collection of int type variables. Here, we are trying to pull the element at index 2 given by variable n, and it goes to index zero of the array.

To achieve this purpose, we pass the array and index into the method ShiftToRight that consists of a for loop. We store our element at index n in a temp variable.

temp=a[n]; i.e. temp=[2] i.e. temp=a[2] i.e. temp=3;

The first iteration:

for(i=2;i>0;i--) i.e. for(i=2;2>0;i--)
a[i]=a[i-1]; i.e. a[2]=a[2-1] i.e. a[2]=a[1] i.e. a[2]=2

The second iteration:

for(i=1;1>0;i--)
a[i]=a[i-1]; i.e. a[1]=a[1-1] i.e. a[1]=a[0] i.e. a[1]=1

As i(0) is not greater than 0, we come out of the for loop.

a[0]=temp i.e. a[0]=3

The elements in the array that comes after the passed index value remains unchanged. In this case, the last two elements remain at their original index after shifting.

import java.util.Arrays;

public class ShiftTest {
    public static void main(String args[]){
        int  [] array = {1,2,3,4,5};
        int n = 2;
        System.out.println("Array "+Arrays.toString(array));
        ShiftToRight(array,n);
    }
    public static void ShiftToRight(int a[],int n){
        int temp = a[n];

        for (int i = n; i > 0; i--) {
            a[i] = a[i-1];
        }

        a[0] = temp;

        System.out.println("Array after shifting to right by one : "+Arrays.toString(a));

    }

}

Output:

Array [1, 2, 3, 4, 5]
Array after shifting to right by one : [3, 1, 2, 4, 5]

Use the skip() Method to Shift an Array in Java 8

The skip() method in Java 8 discards the n number of elements in the stream and returns a stream consisting of the remaining elements. If the stream contains fewer than n elements, it returns an empty stream.

We convert the array into a stream using Arrays.stream() method and call skip() method passing 1 to it. It skips the first element of the array. Later using the lambda format, we print each element of the returned stream.

import java.util.Arrays;
public class TestArray {
    public static void main(String[] args) {
        int [] array = {1,2,3,4,5,6,7};
        Arrays.stream(array).skip(1).forEach(System.out::println);

    }
}

Output:

ShiftedArray: [2, 3, 4, 5, 6, 7]

Use the Collections.rotate(List<type> list, int distance) to Shift an Array in Java

To shift our array by one, we use this method which rotates the elements given in a list of Collection by a given distance. We use Arrays.asList() to convert our array to a list. The list is rotated at index 2.

It moves each element of the list by the specified distance, and the element whose index is more than the given length is moved to the beginning of the list.

import java.util.Arrays;
import java.util.Collections
import java.util.List;

public class Test11 {
    public static void main(String args[]){
        String [] strArray = {"One","Two","Three","Four","Five"};
        System.out.println("Original List : " + Arrays.toString(strArray));

        List<String> list = Arrays.asList(strArray);
        Collections.rotate( list, 2);

        System.out.println("Rotated List: " + list);

    }
}

Output:

Original List : [One, Two, Three, Four, Five]
Rotated List: [Four, Five, One, Two, Three]
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Array