-1

The my goal is to take a user's input and rotate the array however many times based off of their integer input. At first I was trying to get the array to reverse just to see it shift but I have a few errors in my function that won't let me compile.

Edit: I know I used list instead of using arr. I was looking at an example and accidentally typed it in.

Here is my code:

import java.util.Scanner;

public class Project1P2 {
    public static void main(String[] args) {
        int[] arr1 = {2,4,6,8,10,12};
        int[] arr2 = shift(arr1);

        Scanner input = new Scanner(System.in);
        System.out.print("Here is the Array: " + arr1);
        System.out.println("Enter a number to shift array: ");
        int n = input.nextInt();
    }

    public static int[] shift(int[] arr) {
        int[] arrShiftDone = new int[list.length];
        for (int i = 0, j = arrShiftDone.length - 1; i < list.length; i++, j--) {
            arrShiftDone[j] = list[i];
        }
        return arrShiftDone;
    }
}
ApexLameo
  • 1
  • 2

4 Answers4

0

Your mistake is that in your shift method you are using list, it should be arr. I have updated it below. I have also included the shift method. Edited to include negative shifts.

public static void main(String[] args) {
    int[] arr1 = {2, 4, 6, 8, 10, 12};
    int[] arr2 = reverseArray(arr1);

    Scanner input = new Scanner(System.in);
    System.out.println("Here is the Array: " + Arrays.toString(arr1));
    System.out.print("Enter a number to shift array: ");
    int n = input.nextInt();
    int[] arr3 = shiftArray(arr1, n);
    System.out.println("Here is the shifted Array: " + Arrays.toString(arr3));
}
public static int[] reverseArray(int[] arr) {
    int[] arrShiftDone = new int[arr.length];
    for (int i = 0, j = arrShiftDone.length - 1; i < arr.length; i++, j--) {
        arrShiftDone[j] = arr[i];
    }
    return arrShiftDone;
}
public static int[] shiftArray(int[] arr, int shift) {
    int[] arrShiftDone = new int[arr.length];
    shift = shift % arr.length;
    if (shift < 0) {
        shift = arr.length + shift;
    }
    for (int i = 0 + shift, j = 0; j < arr.length; i++, j++) {
        if (i >= arr.length) {
            arrShiftDone[j] = arr[i - arr.length];
        } else {
            arrShiftDone[j] = arr[i];
        }
    }
    return arrShiftDone;
}
Mark Melgo
  • 1,477
  • 1
  • 13
  • 30
0

You need to fix a couple of things:

  1. shift method never gets called from the main method, which means it won't do anything to the array
  2. shift method should have another argument for the number of places to shift, say n
  3. In shift method, you are using list whereas the argument is declared as arr

Below is an example method that shifts the array:

public static int[] shift(int[] arr, int n) {
    if(n > arr.length) 
        n = n%arr.length;

    int[] result = new int[arr.length];

    for(int i=0; i < n; i++){
        result[i] = arr[arr.length-n+i];
    }

    int j=0;
    for(int i=n; i<arr.length; i++){
        result[i] = arr[j];
        j++;
    }

    return result;
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0

The compilation error is because the variable list is unknown. Should be using the argument arr instead of list inside the method shift(int[] arr).

Leo Varghese
  • 165
  • 1
  • 2
  • 12
0

You can use Arrays.stream(int[],int,int) method twice to get two streams with the specified ranges of the array: near and far, then swap them and concat back into one stream, and thus get a shifted array:

public static int[] shiftArray(int[] arr, int n) {
    return IntStream
            .concat(Arrays.stream(arr, n, arr.length),
                    Arrays.stream(arr, 0, n))
            .toArray();
}
public static void main(String[] args) {
    int[] arr1 = {2, 4, 6, 8, 10, 12};

    System.out.println("Source array: " + Arrays.toString(arr1));
    System.out.println("Enter a number: ");

    Scanner input = new Scanner(System.in);
    int n = input.nextInt();

    int[] arr2 = shiftArray(arr1, n % arr1.length);
    System.out.println("Shifted array: " + Arrays.toString(arr2));
}

Output:

Source array: [2, 4, 6, 8, 10, 12]
Enter a number: 
3
Shifted array: [8, 10, 12, 2, 4, 6]

See also: Place positive numbers before negative