我已经编写了一个程序来将int数组向左移动,但是找不到一种方法来将其向右移动。您能否看一下我的代码,并在评论中提出任何想法,如何根据空间数量(int x)“旋转”我的数组右侧,因为目前它只向左移动。谢谢。
public void makeRight(int x) {
int[] anArray = {0, 1, 2, 3, 4, 5};
int counter = 0;
while (counter < x) {
int temp = anArray[0];
for (int i = 0; i < anArray.length - 1; i++) {
anArray[i] = anArray[i + 1];
}
anArray[anArray.length - 1] = temp;
counter++;
}
for (int i = 0; i < anArray.length; i++){
System.out.print(anArray[i] + " ");
}
}