can someone check my code plz
Hello,
I have to make a method that rotates the array to the right
ie if i have an array of int which has the following content: {3, 8, 19, 7}
if i invoke this method it should return {7, 3, 8, 19}
heres my code
Code:
import java.util.*;
public class TestRotateRight
{
public static void main (String[] args)
{
int [] list = {3, 8, 19, 7};
rotateRight(list);
System.out.println(Arrays.toString(list)); // [7, 3, 8, 19]
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
rotateRight(list);
System.out.println(Arrays.toString(list)); // [3, 8, 18, 7]
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
rotateRight(list);
rotateRight(list);
rotateRight(list);
rotateRight(list);
System.out.println(Arrays.toString(list)); // [8, 19, 7, 3]
}
public static void rotateRight (int[] list)
{
int length = list.length;
int [] temp = new int [length];
for ( int i = 0; i < list.length; i++)
{
temp[i] = list[i];
}
list = new int [length];
list[0] = temp[length - 1];
int index = 1;
for ( int i = 0; i < length - 1; i++)
{
list[index] = temp[i];
index++;
}
}
}
i used the debugger on the method that rotates, and i got the correct formation of the new array, but in the main all its printing out is the same list we created it for all the printlns
can someone tell me where my code is wrong at?
thanks in advance