5

这是我到目前为止所拥有的,但是当我运行它时,我得到一个 Java 不匹配错误。这是我的数组:

char[] letters = {'A', 'B' , 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};


/********************************************************************************
    shiftRight() will move the contents of the array one slot to the right
********************************************************************************/
public static void shiftRight( char [] letters )
{
    char last = letters[letters.length-1];          // save off first element

    // shift right
    for( int index =letters.length-1; index >= 0 ; index-- )
        letters[index+1] = letters [index];

    // wrap last element into first slot
    letters[0] = last;
    System.out.print("\nshifted Array: " );
}
4

6 回答 6

12

您可以执行以下操作:

 public static void shiftRight( char [] letters )
    {

        char last = letters[letters.length-1];          // save off first element

        // shift right
        for( int index =letters.length-2; index >= 0 ; index-- )
            letters[index+1] = letters [index];

        // wrap last element into first slot
        letters[0] = last;
        System.out.print("\nshifted Array: " + Arrays.toString(letters) );

    }

我只修改了你的:letters.length-1letters.length-2打印了数组。

另一种更简单的方法是使用,System.arraycopy例如:

last = letters[letters.length-1];
System.arraycopy(letters, 0, letters, 1, letters.length-1 );
letters[0] = last;

要打印数组,您还可以使用:

System.out.print("{");
for (int i=0;i<letters.length-1;i++)
    System.out.print("'"+letters[i]+",");
System.out.println("'"+letters[letters.length-1]+"'}");
于 2012-10-29T20:27:32.400 回答
1

letters[index+1]在 for 循环中第一次执行时(when index = letters.length-1),它指向letters[letters.length]的不是一个有效的索引,因为索引从0length-1

更新您的 for 循环以开始indexletters.length-2确保您的array.length>1. 也就是说:

   if(letters.length > 1){ //make sure array has minimum two elements
      // shift right
      for( int index = letters.length-2; index >= 0 ; index-- ){
          letters[index+1] = letters [index];
      }
   }

最后,您可以将数组打印为:

   System.out.println("Shifted Array: " +letters); 

编辑:示例工作代码。

如果您将数组设置为Character数组,例如下面的数组main(仅用于帮助打印)

    Character[] letters = {'A', 'B' , 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};

然后将其传递给shiftRight具有更新代码的方法,如下所示:

  public static void shiftRight( Character [] letters )
  {
    Character last = letters[letters.length-1];  
    if(letters.length >1){ //make sure array has minimum two elements
        // shift right
        for( int index =letters.length-2; index >= 0 ; index-- ){
            letters[index+1] = letters [index];
        }
     }
    letters[0] = last;
    System.out.println(Arrays.toString(letters));
    //^ prints: Shifted Array: [J, A, B, C, D, E, F, G, H, I]
  }

你应该准备好了。

于 2012-10-29T20:24:57.740 回答
0

添加

     if(index<letters.length-1)  condition 

在你的 for 循环之前

 letters[index+1] = letters [index]

如果您不检查if(index<letters.length-1)条件,您将在此处的最后一个索引处获得 ArrayIndexOutOfBounds 异常letters[index+1] = letters [index]

于 2012-10-29T20:24:52.183 回答
0

ArrayIndexOutOfBoundsException实际上,只要您想阅读(在循环内),您就会获得:

letters[index+1]

原因是您的index变量初始化:index = letters.length-1

所以letters[index+1] == letters[letters.length] 但逻辑上letters.length导致 ArrayIndexOutOfBoundsException.

在不更改逻辑的情况下更正代码的简单方法:

for(int index = letters.length-1; index > 0 ; index--)
   letters[index] = letters [index-1];

注意操作符符号>而不是>=。否则,这也会导致相同的Exception.

此外,为了显示最终数组,请使用:Arrays.toString(letters)

我什至写了我的解决方案来保持你的逻辑:

public static void main(String[] args) {
    char[] letters = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'};
    printShiftedLetters(shiftElementsToRight(letters));
}

public static char[] shiftElementsToRight(char[] array) {
    if (array == null) {
        throw new NullPointerException("array must not be null");
    }
    if(array.length <= 1){
        return array;
    }
    char[] shiftedArray = new char[array.length];
    char lastElement = array[array.length - 1];
    for (int i = array.length - 1; i > 0; i--) {
        shiftedArray[i] = array[i - 1];
    }
    shiftedArray[0] = lastElement;
    return shiftedArray;
}

private static void printShiftedLetters(char[] shiftedLetters) {
    System.out.println(Arrays.toString(shiftedLetters));
}
于 2012-10-29T20:50:44.377 回答
0

以下解决方案对我有用。

   public void rightShiftArray(int[] arr, int n){
        int[] output=new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            int newLocation = (i+(arr.length+n))%arr.length;
            output[newLocation] = arr[i];
        }
        for (int i = 0; i < output.length; i++) {
            System.out.print(output[i]+",");
        }
    }

arr -> 要移动的数组 n -> 将元素移动多少位置。

于 2021-09-05T14:44:38.827 回答
0

对于整数值

您可以对整数值执行类似的操作:

public static void main(String[] args) {
    int arr[] = {1,2,3,4,5};
    arrayReorder(arr,25);
}



private static void arrayReorder(int[] arr, int repeat) {
    int count = 0;
    int length = arr.length;
    int end = 0;
    int traversedArray[] = new int[length];

    while (count < repeat) {
        end = arr[length-1];
        for (int i = 1; i < length; i++) {
            traversedArray[i] = arr[i-1];
            traversedArray[0] = end;
        }
        System.out.println();
        for (int a : traversedArray) {
            System.out.print(a + " -> ");
        }
        arr = traversedArray.clone();
        count++;
    }
}
于 2020-01-08T09:05:29.297 回答