BipoN
BipoN

Reputation: 93

Unable to think of a way to shift array

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.

Upvotes: 3

Views: 125

Answers (3)

Vimal
Vimal

Reputation: 404

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.

Upvotes: 0

fedup
fedup

Reputation: 1259

Use Java's convenience methods. Most people still want to write for loops. Basically, you need to save off the elements you are overwriting with the shift. Then place those saved ones back in the array. System.arraycopy is nice in that it takes care of some nasty parts of moving elements in an array.

void shift(int shiftBy, int... array) {
    int[] holdInts = Arrays.copyOf(array, shiftBy);
    System.arraycopy(array, shiftBy, array, 0, array.length - shiftBy);
    System.arraycopy(holdInts, 0, array, array.length - shiftBy, holdInts.length);
}

Upvotes: 1

Lewis921
Lewis921

Reputation: 37

In order to shift the numbers in the array, the following for loop works for shifting the values within the array.

// prerequisite: array is already filled with values
for(int i = 0; i < numArray.length; i++) {
    arr[i] += shiftNum;

    if (numArray[i] > 30) { 
        numArray[i] -= 30;
    } else if (numArray[i] <= 0) {
        numArray[i] += 30;
    }
}

According to you code, the array created will contain value from 1 - 30 including 1 and 30. If you want your code to contain values from 0 - 29 instead, change numArray[i] > 30 to numArray[i] >= 30 and change numArray[i] <= 0 to numArray[i] < 0.

Upvotes: 3

Related Questions