- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to cyclically rotate an array by one.
To rotate the contents of an array cyclically −
- create an empty variable. (temp)
- save the last element of the array in it.
- Now, starting from the nth element of the array, replace the current element with the previous element.
- Store the element in temp in the 1st position.
Example
import java.util.Arrays; import java.util.Scanner; public class CyclicallyRotateanArray { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter the required size of the array ::"); int size = sc.nextInt(); int [] myArray = new int[size]; System.out.println("Enter elements of the array"); for(int i=0; i< size; i++){ myArray[i] = sc.nextInt(); } System.out.println("Contents of the array: "+Arrays.toString(myArray)); int temp = myArray[size-1]; for(int i = size-1; i>0; i--){ myArray[i] = myArray[i-1]; } myArray[0] = temp; System.out.println("Contents of the cycled array: "+Arrays.toString(myArray)); } }
Output
Enter the required size of the array :: 5 Enter elements of the array 14 15 16 17 18 Contents of the array: [14, 15, 16, 17, 18] Contents of the cycled array: [18, 14, 15, 16, 17]
- Related Articles
- Java program to program to cyclically rotate an array by one
- Python program to cyclically rotate an array by one
- Golang Program to Rotate Elements of an Array
- Golang Program to cyclically permutes the elements of the array
- Python program to left rotate the elements of an array
- Python program to right rotate the elements of an array
- Java Program to Rotate Matrix Elements
- PyTorch – How to rotate an image by an angle?
- Java Program to Rotate Elements of a List
- Java program to reverse an array
- How to rotate an array k time using C#?
- Python program to right rotate a list by n
- Write a program in Java to rotate a matrix by 90 degrees in anticlockwise direction
- How to rotate an image with OpenCV using Java?
- Python program to rotate doubly linked list by N nodes

Advertisements