Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
shift
java
communitycreator

What is ArrayUtils.shift in Java?

abhilash

shift is a static method of the ArrayUtils class that shifts the elements in an array in the clockwise or anti-clockwise direction by an offset.

  • The shift operation is in-place in nature, i.e., the modification is done to the original array.
  • Positive offset leads to clockwise rotation/shift of the elements.
  • Negative offset leads to anti-clockwise rotation/shift of the elements.
  • If the offset value is greater than the array’s length, then the effective offset is (offset) modulo (length of the array).

Example 1

  • array = [1,2,3,4,5]
  • offset = 2

Application of the shift function will result in the following array: [4,5,1,2,3].

As the offset is positive, the rotation/shift is in the clockwise direction. The last two elements are moved to the beginning of the array.

Example 2

  • array = [1,2,3,4,5]
  • offset = -2

Application of the shift function will result in the following array: [3,4,5,1,2].

As the offset is negative, the rotation/shift is in the anti-clockwise direction. The first two elements are moved to the end of the array.

Example 3

  • array = [1,2,3,4,5]
  • offset = 34

Application of the shift function will result in the following array: [2,3,4,5,1].

As the offset is positive, the rotation/shift is in the clockwise direction. Here, the effective offset value is 34 % 5 = 4. The last four elements of the array are moved to the beginning of the array.

ArrayUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven Project, add the following dependency to the pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the ArrayUtils class as follows.

import org.apache.commons.lang3.ArrayUtils;

Syntax

public static void shift(final int[] array, final int offset)

Parameters

  • final int[] array: the array
  • final int offset: the numbers of positions to rotate/shift the elements

Return value

The method doesn’t return anything as the shifting is done to the original array that is passed as an argument.

Code

import org.apache.commons.lang3.ArrayUtils;

public class Main {

    public static void main( String args[] ) {
        int[] array = {1,2,3,4,5};
        System.out.print("Original Array - ");
        for(int i: array){
            System.out.print(i + " ");
        }
        ArrayUtils.shift(array, 34);
        System.out.print("\nModified Array after shifting - ");
        for(int i: array){
            System.out.print(i + " ");
        }
    }
}

Expected output

Original Array - 1 2 3 4 5 
Modified Array after shifting - 2 3 4 5 1

RELATED TAGS

array
shift
java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring