【发布时间】:2015-11-16 23:39:24
【问题描述】:
所以我试图将一个数组向右移动,以便 x 显示为 4、5、6、7、1、2、3。我认为我的算法是正确的,但由于某种原因它不会返回x 并跳过“x = rotate(x, 3)”行之后的任何内容。任何帮助或解释将不胜感激。
public class Ex1sub3 {
public static void main(String[] args){
double[] x = {1, 2, 3, 4, 5, 6, 7};
System.out.println("Before rotation: ==============================");
for (int i = 0; i < x.length; i++)
{
System.out.println("x[" + i + "]: " + x[i]);
}
x = rotate(x, 3);
System.out.println("After rotation:==============================");
for (int i = 0; i < x.length; i++)
{
System.out.println("x[" + i + "]: " + x[i]);
}
}
private static double[] rotate(double[] x, int n){
int l = x.length;
double [] r = x;
int c = 0;
int rotation;
int startRotation = 0;
for (c = 0; c < l; c++)
{
rotation = c+n;
while (rotation < l-n)
{
x[c] = r[rotation];
}
if (rotation >= l-n)
{
x[c] = r[startRotation];
startRotation++;
}
}
return x;
}
}
【问题讨论】:
标签: java arraylist elements shift