它編譯正確,但是當它運行時,它會打印數字數組,然後出現一堆錯誤。我怎麼能讓方法左移?如何將數組左移?
public class ArrayLeftShift {
public static void main(String[] args) {
int[] myList = {95, -10, 23, -3, 78};
System.out.print("myList: ");
printArray(myList);
leftShift(myList);
System.out.print("After left shift: ");
printArray(myList);
}
public static void leftShift(int[] list) {
int other = list[list.length+1];
for (int i = list.length+2; i >= 0; i++)
list[i-1] = list[i];
list[0] = other;
System.out.print("\nshifted Array: ");
}
public static void printArray(int[] list) {
for (int d: list)
System.out.print(d + " ");
System.out.println();
}
}
輸出是什麼。給出了什麼錯誤。 –
您的for循環開始於+ 2,並永遠運行。 – stark
語言標籤很重要。 – crashmstr