Advertisement
CoineTre

JF-ExcArrays04.ArraysRotation

Jan 20th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. /*
  2. Write a program that receives an array and number of rotations you have to perform (first element goes at the end) Print the resulting array.
  3. */
  4. import java.util.Scanner;
  5. public class Exc04ArrayRotation {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String[] arr = scanner.nextLine().split(" ");
  9.         int nRotations = scanner.nextInt();
  10.         for (int i = 0; i < nRotations; i++) {
  11.             String tempValue = arr[0];
  12.             for (int j = 0; j < arr.length - 1; j++) {
  13.                 arr[j] = arr[j+1];
  14.             }
  15.             arr[arr.length -1] = tempValue;
  16.         }
  17.         System.out.println(String.join(" ",arr));
  18.     }
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement