2017-01-10 36 views
-1

我想將此數組中的所有元素移位1:[1 4 9 16 25]最後一個元素(25)成爲第一個元素。我的問題是,我不斷讓數組打印出來[25 1 1 1 1],我該如何解決這個問題?如何移動數組1中的元素?

import java.util.Arrays; 

public class question4 { 
public static void main(String[] args){ 
    int[] array = new int[5]; 
    array[0] = 1; 
    array[1] = 4; 
    array[2] = 9; 
    array[3] = 16; 
    array[4] = 25; 
    ShiftNumbers(array); 
    System.out.print(Arrays.toString(array)); 
} 
public static void ShiftNumbers(int[] array){ 
    int temp = array[array.length-1]; 
    for(int i=0; i<=array.length-2; i++){ 
     array[i+1]=array[i]; 
    } 
    array[0] = temp;   



} 

}

+1

嘗試從末尾向後工作,而不是從一開始就向前。否則,您將一直複製相同的值。 –

+0

你想在原地做這個(有一個數組)還是使用多個數組可以嗎?後一種方法變得非常簡單直接,但都不困難。 – Makoto

+0

謝謝!我沒有意識到它在這樣的循環中覆蓋了值。 – EvanZ

回答

0

你的問題是,當你從數組的開始,它會覆蓋價值的其餘所有循環,我建議從數組的末尾去數組的時候開始覆蓋數組值。 試試這個:

import java.util.Arrays; 

public class question4 { 
public static void main(String[] args){ 
    int[] array = new int[5]; 
    array[0] = 1; 
    array[1] = 4; 
    array[2] = 9; 
    array[3] = 16; 
    array[4] = 25; 
    ShiftNumbers(array); 
    System.out.print(Arrays.toString(array)); 
} 
public static void ShiftNumbers(int[] array){ 
    int temp = array[array.length-1]; 
    for(int i=array.length-2; i>=0; i--){ 
     array[i+1]=array[i]; 
    } 
    array[0] = temp;   

} 
} 
+0

謝謝你!我明白我現在做錯了什麼。 – EvanZ

1

更新了答案,給一個完全工作的解決方案,而不僅僅是指導的基礎上,從索蒂里奧斯Delimanolis評論。

隨着System.arrayCopy,你可以很容易地做到了3個步驟。

public static int[] revisedShiftNumbers(int[] array) { 
    int[] newArr = new int[array.length]; 
    System.arraycopy(array, 0, newArr, 1, array.length - 1); 
    newArr[0]=array[array.length-1]; 
    return newArr; 
} 

然後調用此方法如int[] shiftedArray = revisedShiftNumbers(array);

該方法採用五個參數:

  1. src:源陣列。
  2. srcPosition:在您想要開始的位置源 複製。
  3. des:目標數組。
  4. desPosition:目標數組中的位置應該開始複製 的位置。
  5. length:要複製的元素數量。
+1

我不知道誰降低了這一點,但這是最有效的方法。 – Enzokie

+0

@Enzokie我想知道是誰加了這個,因爲所提供的代碼拋出了一個'ArrayIndexOutOfBoundsException'。它也轉向所要求的相反方向。 –

+0

@Sotirios,你沒有得到解決方案的本質。目的不是要提供OP可以插入他的代碼的代碼,並且不需要任何額外的努力就可以開始工作。這個想法是建議使用'System.arrayCopy'來實現操作的目標。 – VHS

0

代碼中的問題是for loop裏面用當前值覆蓋下一個值,它會給出25 1 1 1 1。由於所有其他值都丟失(覆蓋)。

現在,我使用模塊化算法來解決這個問題。

import java.util.Arrays; 

public class question4 { 

    public static void main(String[] args){ 
     int[] array = {1, 4, 9, 16, 25}; 
     ShiftNumbers(array); 
     System.out.print(Arrays.toString(array)); 
    } 

    public static void ShiftNumbers(int[] array){ 

     int last = array[0]; 
     int temp; 
     int length = array.length; 

     for(int i = 0; i < array.length; i++){ 

      temp = array[(i+1)%length]; 
      array[(i+1)%length] = last; 
      last = temp; 

     } 
     array[0] = temp;   

    } 
} 
0

其他解決方案。使用linkedList。 :-)

public class ShiftSimple { 
    private static int mTestCnt = 0; 
    public static void main(String[] args) { 
     int[] array = new int[5]; 
     array[0] = 1; 
     array[1] = 4; 
     array[2] = 9; 
     array[3] = 16; 
     array[4] = 25; 

     shiftNumbers(array); 
     printNumbers(array); 

     shiftNumbers(array); 
     printNumbers(array); 

     shiftNumbers(array); 
     printNumbers(array); 
    } 

    public static void shiftNumbers(int[] array) { 
     LinkedList<Integer> linkedList = new LinkedList<Integer>(); 
     for(int item : array) { 
      linkedList.add(item); 
     } 
     linkedList.push(linkedList.pollLast());  // shift last to first 

     int i = 0; 
     for(int item : linkedList) { 
      array[i++] = item; 
     } 
    } 

    public static void printNumbers(int[] array) { 
     System.out.println("["+ ++mTestCnt +"] Check Numbers"); 
     for(int i : array) { 
      System.out.println(">> " + i); 
     } 
    } 
} 

[OUTPUT]

[1] Check Numbers 
>> 25 
>> 1 
>> 4 
>> 9 
>> 16 
[2] Check Numbers 
>> 16 
>> 25 
>> 1 
>> 4 
>> 9 
[3] Check Numbers 
>> 9 
>> 16 
>> 25 
>> 1 
>> 4 
0

嘗試這個

public static void main(String[] args) { 
    int[] array = new int[5]; 
    array[0] = 1; 
    array[1] = 4; 
    array[2] = 9; 
    array[3] = 16; 
    array[4] = 25; 

    ReverseArray(array); 
    System.out.print(Arrays.toString(array)); 
} 

public static void ReverseArray(int[] a) { 
    int size, i, j, temp; 
    size = a.length; 

    for(i=0; i<size; i++) 
    { 
     a[i] = a[i]; 
    } 

    j = i - 1;  // now j will point to the last element 
    i = 0;   // and i will point to the first element 

    while(i<j) 
    { 
     temp = a[i]; 
     a[i] = a[j]; 
     a[j] = temp; 
     i++; 
     j--; 
    } 

    System.out.print("\n Now the Reverse of Array is : \n"); 
    for(i=0; i<size; i++) 
    { 
     System.out.print(a[i]+ " "); 
    } 
}