Java Program to Left Rotate Elements of Array

Hello guy’s. here is the post to move array elements into left or right direction. the result can be achieve by loops. we can use any loops like for , for-each,while etc… let’s see the program.

  • Output Screen

Code Explanation :
step 1 : if you debug/run the program then control goes to main method thereafter that go inside of the main.
step 2 : i have taken 1 integer array variables a. initialize numbers with 8,6,4,21. int[] a = new int[] {8,6,4,21};
step 3 : next control move to move method and compiler send control to move method block.move(a);
step 4 : now arr receive all values of a and now control move to move method.static void move(int[] arr).
step 5 : there i have taken two integer variable t,temp.int t = arr.length;int temp; t= 4.
step 6 : now control move to for method. for(int j = t-1; j > 0; j–) j = 4-1=3. 3>0 true.

step 7 : temp = arr[t-1]; temp = arr[4-1];temp = arr[3];temp = 21;
step 8 : arr[t-1] = arr[j-1]; arr[3] = arr[2]; arr[3] = 4
step 9 : arr[j-1] = temp; arr[2] = 21; next j– , j = 2. and loop continue until false.
step 10 : after doing left rotation arr values go to n. for(int n : arr)
step 11 : it prints all n values. System.out.print(n + ” “);
step 12 : next control terminate move method and control back to move(a); method.

Happy Coding… Thanks.

Post Author: adama