Unable to think of a way to shift array

BipoN :

I am stuck and can't think of a way to properly shift an array by __ units. I am trying to create an array of 30 items (numbers 1-30) which can then be shifted to the right by the number the user inputs. This would mean that the first few numbers in the array would take the index's at the end of the array, and the rest of the numbers would be shifted to the left. (Ex, if shift = 3, numbers 1,2,3 would take the index of 27,28,29, and the rest of the numbers 4-30 would shift left making index 0 =4, index 1=5, index 2=6....

import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner (System.in);

    System.out.println("\nEnter the shift/rotation:");
    int shiftNum = input.nextInt();

    int [] numArray = new int [30];

    for(int i = 0; i < 30; i++){
        numArray [i] = i+1;
        System.out.print(numArray[i]+" ");
    }

  }
}

This is the code I have so far, any suggestions to how I can do this? I have tried to make a separate for loop like

numArray [i-shiftNum] = numArray[i];

But when doing this, the index of 0-shiftNum would be negative and would not work. This is the context of the problem:

Create a program that will create an array of 30 items. Then it will rotate the array by a number selected by the user.

Vimal :

Here is quick fix for you. Please check following code.

Input :

Enter the shift/rotation: 4

Output :

Rotate given array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]

After Rotate [27, 28, 29, 30, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26]

    public static void main(String[] args) {
    RotationDemo rd = new RotationDemo();
    int[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};
    int k = 0;
    Scanner scan = new Scanner (System.in);
    try{
         System.out.println("\nEnter the shift/rotation:");
         int shiftNum = scan.nextInt();
         if(shiftNum < 30) {
             k = shiftNum;
             System.out.println("Rotate given array " + Arrays.toString(input));
             int[] rotatedArray = rd.rotateRight(input, input.length, k);
             System.out.println("After Rotate  " + 
                  Arrays.toString(rotatedArray));
         } else {
            System.out.println("Shift number should be less than 30");
         }
         } catch(Exception ex){
         } finally {
            scan.close();
        }
      }
      public int[] rotateRight(int[] input, int length, int numOfRotations) {
        for (int i = 0; i < numOfRotations; i++) {
          int temp = input[length - 1];
          for (int j = length - 1; j > 0; j--) {
            input[j] = input[j - 1];
          }
          input[0] = temp;
        }
        return input;
      }

Hope this example works.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322618&siteId=1