TheBulgarianWolf

Array Rotation

Apr 27th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Arrays;
  3.  
  4. public class SoftUni {
  5.    
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         System.out.print("Enter your array here: ");
  9.         String[] stringArr = sc.nextLine().split(" ");
  10.         int[] intArr = Arrays.stream(stringArr).mapToInt(e -> Integer.parseInt(e)).toArray();
  11.         System.out.print("Enter the number of rotations you want(1st element becomes last with a rotation performed): ");
  12.         int rotations = Integer.parseInt(sc.nextLine());
  13.         for(int i = 0;i<rotations;i++){
  14.             int temp = intArr[0];
  15.             for(int m = 0;m<intArr.length-1;m++){
  16.                 intArr[m] = intArr[m+1];
  17.             }
  18.             intArr[intArr.length-1] = temp;
  19.         }
  20.         for(int i: intArr){
  21.             System.out.print(i + " ");
  22.         }
  23.     }
  24.  
  25. }
Add Comment
Please, Sign In to add comment