In the previous article, we have seen Java Program to Find all the Combination of Four Elements Where Sum of All the Four Elements are Equal to a Specified Number
In this article we are going to see how to cyclically rotate an array clockwise by one using Java Programming Language.
Java Program to Cyclically Rotate a Given Array Clockwise by One
Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to cyclically rotate an array clockwise by one.
Method-1: Java Program to Cyclically Rotate a Given Array Clockwise by One By Static Initialization of Array Elements
Approach:
- Declare and initialize an array.
- Store the last element of the array in a temp variable.
- Iterate over the array and shift the elements by one place to the right.
- Store the temp variable at the 0th index.
Program:
public class Main
{
public static void main(String[] args)
{
// initializing array
int[] arr = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println("The array is : ");
printArray(arr);
// calling the function
rotate_by_one(arr);
System.out.println("After rotation the array is : ");
printArray(arr);
}
static void rotate_by_one(int arr[])
{
// initializing variables
int temp = arr[arr.length - 1];
// looping through the array shifting the elements
for (int i = arr.length - 1; i > 0; i--)
{
arr[i] = arr[i - 1];
}
//Storing tempvalue at 0th index of array.
arr[0] = temp;
}
//printArray() method to print the array
static void printArray(int[] arr)
{
// printing array
for (int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println("");
}
}
Output: The array is : 1 2 3 4 5 6 7 After rotation the array is : 7 1 2 3 4 5 6
Method-2: Java Program to Cyclically Rotate a Given Array Clockwise by One By Dynamic Initialization of Array Elements
Approach:
- Ask use length of the array.
- Initialize the array with given size.
- Ask the user for array elements.
- Store the last element of the array in a temp variable.
- Iterate over the array and shift the elements by one place to the right.
- Store the temp variable at the 0th index
Program:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
//Scanner class object created
Scanner sc = new Scanner(System.in);
// asking user to enter the number of elements
System.out.println("Enter number of elements in the array : ");
int n = sc.nextInt();
// initializing the array
int[] arr = new int[n];
// asking user to enter the elements
System.out.println("Enter elements of the array : ");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("The array is : ");
printArray(arr);
// calling the function
rotate_by_one(arr);
System.out.println("After rotation the array is : ");
printArray(arr);
}
static void rotate_by_one(int arr[])
{
// initializing variables
int temp = arr[arr.length - 1];
// looping through the array shifting the elements
for (int i = arr.length - 1; i > 0; i--)
{
arr[i] = arr[i - 1];
}
//Storing tempvalue at 0th index of array.
arr[0] = temp;
}
//printArray() method to print the array
static void printArray(int[] arr)
{
// printing array
for (int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.println("");
}
}
Output: Enter number of elements in the array : Enter elements of the array : The array is : 1 2 3 4 5 After rotation the array is : 5 1 2 3 4
Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.
Related Java Programs:
- Java Program to Arrange the Elements of a Given Array of Integers Where All Negative Integers Appear Before All the Positive Integers
- Java Program to Separate 0s on Left Side and 1s on Right Side of an Array of 0s and 1s in Random Order
- Java Program to Separate all Even Numbers First and Then Odd Numbers
- Java Program to Check if a Sub Array is Formed by Consecutive Integers from a Given Array of Integers