Sorapalli Venkatesh
Sorapalli Venkatesh

Reputation: 59

Left Rotation using Java

Here I am Getting an ERROR

Cannot make a static reference to the non-static method leftRotatebyOne(int[], int) from the type LeftRotation

Here is my code..

public static int[] arrayLeftRotation(int[] arr, int n, int k) 
{
 int i;
 for (i = 0; i < k; i++)
    leftRotatebyOne(arr, n); 
}

void leftRotatebyOne(int arr[], int n) 
{
    int i, temp;
    temp = arr[0];
    for (i = 0; i < n - 1; i++)
        arr[i] = arr[i + 1];
    arr[i] = temp;
}

public static void main(String[] args)
{
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int k = in.nextInt();
    int a[] = new int[n];
    for(int a_i=0; a_i < n; a_i++)
    {
        a[a_i] = in.nextInt();
    }

    int[] output = new int[n];
    output = arrayLeftRotation(a, n, k);

    for(int i = 0; i < n; i++)
        System.out.print(output[i] + " ");
}

Can anybody tell to proceed me further.

Upvotes: 1

Views: 458

Answers (1)

Pooja Arora
Pooja Arora

Reputation: 574

In the main method you are calling arrayLeftRotation method (which is static) which in turn is calling leftRotatebyOne, here leftRotatebyOne is non-static method, which is being called through a static method which is not allowed in java.

Change the declaration of the method leftRotatebyOne to static as follows

static void leftRotatebyOne(int arr[], int n)

Upvotes: 3

Related Questions