Java Program for array rotation


An array is a linear data structure that is used to store a group of elements with similar datatypes. It stores data in a sequential manner. Once we create an array we can't change its size i.e. it can store a fixed number of elements. The array comes with a vast application and use case. Also, we can perform numerous operations on arrays. This article will help you to understand the basics of arrays and also, we will make java programs to perform right and left rotation operations on arrays.

Java Program for Array Rotation

First, let's understand the term right and left rotation in the context of an array.

In the right rotation of an array, we simply shift the elements of the array to our right till the specified number of positions and vice versa in the case of left rotation as illustrated in the next example.

Instance

Syntax to declare an array

Data_Type nameOfarray[]; // declaration
Or,
// declaration with size
Data_Type nameOfarray[] = new Data_Type[sizeofarray];

We can use any of the above syntaxes in our program.

Before jumping to the program directly, let's understand a built-in method named 'System.arraycopy()' that we are going to use in our example programs.

System.arraycopy() method

The java.lang.System.arraycopy() is a static method of Java System class that is used to copy a source array from a specified index to the specified index of the destination array.

Syntax

System.arraycopy(srcArray, index1, destArray, index2, length);

Here,

  • srcArray − The array to be copied.

  • index1 − Starting index of source array from where we require to copy.

  • index2 − Starting index of destination array to where the elements will be copied.

  • destArray − Array where elements will be copied.

  • length − Number of elements to be copied.

Example 1

In the following example, we will rotate the array two times to our left from the second index.

Approach

  • First, define a method along with two parameters that accept an integer and index as an argument.

  • Inside this method, create a temporary array to store rotated elements. Then, use the 'arraycopy()' method to copy original array to a temporary array from the specified index.

  • Again, use the 'arraycopy()' method to copy the remaining elements of original array to temporary array so that the elements get shifted to the left. Now, use the 'arraycopy()' method one more time to copy all the rotated elements from the temporary array to original array.

  • In the main() method, declare and initialize an integer array and then, initialize an integer variable to specify the index from where we want to rotate. Then, call the user-defined method to rotate the array.

import java.util.Arrays;
public class ArrayRotationleft {
   // user-defined method to rotate the given array
   public static void rotateArray(int[] arr, int rotateBy) {
      int length = arr.length;
      // Calculate the effective rotation value
      int rotation = rotateBy % length;
      // Create a temporary array to store rotated elements
      int[] temp = new int[rotation];
      // Copy elements till the rotateBy to the temporary array
      System.arraycopy(arr, 0, temp, 0, rotation);
      // Shift the remaining elements to the left
      System.arraycopy(arr, rotation, arr, 0, length - rotation);
      // Copy the rotated elements from the temporary array to the original array
      System.arraycopy(temp, 0, arr, length - rotation, rotation);
   }
   public static void main(String[] args) {
      // declaring and initializing an array
      int[] arr = { 5, 7, 89, 91, 34, 21, 11, 0 };
      int rotateBy = 2;
      System.out.println("The original array is as follows: ");
      // for-each loop to print original array 
      for(int print : arr) {
         System.out.print(print + " ");
      }
      System.out.println();
      System.out.println("The array after left rotation: ");
      // calling the method to rotate the array
      rotateArray(arr, rotateBy);
      // for-each loop to print rotated array
      for(int print : arr) {
         System.out.print(print + " ");
      }
   }
}

Output

The original array is as follows: 
5 7 89 91 34 21 11 0 
The array after left rotation: 
89 91 34 21 11 0 5 7

Example 2

To perform the right rotation, instead of copying the original array from 0th index, we need to copy the elements from where we want to rotate the array.

import java.util.Arrays;
public class ArrayRotationright {
   public static void main(String[] args) {
      // declaring and initializing an array
      int[] arr = { 5, 7, 89, 91, 34, 21, 11, 0 };
      int rotateBy = 2;
      System.out.println("The original array is as follows: ");
      // for-each loop to print original array 
      for(int print : arr) {
         System.out.print(print + " ");
      }
      System.out.println();
      System.out.println("The array after right rotation: ");
      // calling the method to rotate the array
      rotateArray(arr, rotateBy);
      // for-each loop to print rotated array
      for(int print : arr) {
         System.out.print(print + " ");
      }
   }
   // user-defined method to rotate the given array
   public static void rotateArray(int[] arr, int rotateBy) {
      int length = arr.length;
      // Calculate the effective rotation value
      int rotation = rotateBy % length;
      // Create a temporary array to store rotated elements
      int[] temp = new int[rotation];
      // Copy the elements till rotateBy to the temporary array
      System.arraycopy(arr, length - rotation, temp, 0, rotation);
      // Shift the remaining elements to the right
      System.arraycopy(arr, 0, arr, rotation, length - rotation);
      // Copy the rotated elements from the temporary array to the original array
      System.arraycopy(temp, 0, arr, 0, rotation);
   }
}

Output

The original array is as follows: 
5 7 89 91 34 21 11 0 
The array after right rotation: 
11 0 5 7 89 91 34 21

Conclusion

In this article, we have understood what is an array and discussed two Java programs to perform right and left rotation of an array. Also, we discovered arraycopy() method that is very useful in copying elements from one array to another array.

Updated on: 10-Aug-2023

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements