2014-09-18 73 views
2

我正在製作一個跟蹤比賽的程序。我希望能夠將一場比賽從原來的位置移開一定數量的位置,然後將所有的位置都移到下面。將數組中的元素移動到其他數組中

我有一個「Rounds」數組,每個「Round」都有一個「Races」數組。

Round[] rounds = {new Round(), new Round(), new Round()};

每輪具有種族的陣列。

Race[] races = {new Race(), new Race(), new Race()};

我也可以代表這樣的:

0.0, 0.1, 0.2; 1.0, 1.1, 1.2; 2.0, 2.1, 2.2

我想借此0.2對象,並在向前推進3點1.22.0之間。請記住,這樣做會移動數組之間的對象,因此必須將所有內容移動到三個數組之間。因此,它看起來像這樣移動後:

0.0, 0.1, 1.0; 1.1, 1.2, 0.2; 2.0, 2.1, 2.2

再次,這是移動不是在同一個陣列之間和對象。

+0

你需要一個鏈接列表在給定的所需操作我想,而不是數組或二維數組。如果您可以指定一輪中包含的比賽數量,並且您需要隨時調整比賽,請嘗試我的方法。 – HuStmpHrrr 2014-09-18 16:49:48

+0

我想你可能想要維護不同的參考單個回合。在這種情況下,你不需要所有比賽中的所有比賽的單個鏈表。但是,將數組更改爲列表中的輪次和比賽將有助於記帳,因爲list.add(0)將自動處理向上推送現有條目索引。移除工作的方式相同,只需從一個列表中刪除,然後再添加到另一個列表中,就可以很容易地在一輪之間移動一次比賽。 – JDS 2014-09-18 16:57:02

+0

@HuStmpHrrr我不會一直這樣做,只是在賽車手尚未準備好的情況下,我可以稍後再調整。 – 2014-09-18 16:58:50

回答

1

這是你可以做的事。這是解決您的直接問題的方法,請查看其他解決方案的註釋,這些解決方案可能更簡單,更高效。

你可以組織一個數組的數組(矩陣差不多),使得外部陣列的各項指標符合您的陣列中的一個:

index 
    0 [0.0, 0.1, 0.2] 
    1 [1.0, 1.1, 1.2] 
    2 [2.0, 2.1, 2.2] 

現在我們必須遊移數據。這是可以做到這樣的事:

  1. 保存您正在進入一個臨時變量
  2. 移陣列中的所有數據你的元素從
  3. Shift鍵在與陣列移動的所有數據元素陣列正在從陣列中移動和陣列你正在向
  4. 移位所有數據你在元件保存的元件從所述臨時變量移動到
  5. 商店適當位置

代碼:

void move(double[][] arrays, int indexFrom, int posFrom, int indexTo, int posTo) { 

    // step 1 
    double movedElement = arrays[indexFrom][posFrom]; 

    // step 2 
    // shift all elements that are to the right of the moved element by 1 position left 
    for(int j = posFrom + 1; j < arrays[indexFrom].length; j++) { 
     arrays[indexFrom][j - 1] = arrays[indexFrom][j]; 
    } 

    // step 3 
    // shift all arrays between the array you are moving from 
    // and the array you are moving to 
    for(int i = indexFrom + 1; i < indexTo; i++) { 
     // move the first element of the next array 
     // as the last element of the previous array 
     int indexOfLast = arrays[i-1].length - 1; 
     arrays[i - 1][indexOfLast] = arrays[i][0]; 
     // shift remaining elements of the next array 
     for(int j = 1; j < arrays[i].length; j++) { 
      arrays[i][j - 1] = arrays[i][j]; 
     } 
    } 

    // step4 

    // store the first element of the array we are moving to 
    // as the last element of the previous array 
    int indexOfLast = arrays[indexTo - 1].length - 1; 
    arrays[indexTo - 1][indexOfLast] = arrays[indexTo][0]; 

    // starting from the position we are moving to, shift all elements 
    // to the left  
    for(int j = 1; j <= posTo; j++) { 
     arrays[indexTo][j - 1] = arrays[indexTo][j]; 
    } 

    // step 5 
    // store the moved element at its proper position 
    arrays[indexTo][posTo] = movedElement; 
} 

調用函數從位置2陣列0內陣列1內移動元件到位置2:

move(data, 0, 2, 1, 2); 

在輸入:

| 0.0 0.1 0.2 | 
| 1.0 1.1 1.2 | 
| 2.0 2.1 2.2 | 

可生產輸出:

| 0.0 0.1 1.0 | 
| 1.1 1.2 0.2 | 
| 2.0 2.1 2.2 | 

Click for the full running test code

+0

我想這是我正在尋找的東西,讓我試試看,當我回家。 – 2014-09-18 17:29:10

+0

這很好,謝謝! – 2014-09-19 02:04:36

+0

@ChristopherSmith歡迎您:) – nem035 2014-09-19 02:44:17

0

一個可能的辦法只是在頂層2個陣列:首先是種族的數組,其中包含所有的比賽的:

Race[] races = {new Race(), ...}; 

另一種是每一輪的的索引數組開始比賽。在你的例子中它會是:

int[] roundStartIndicies = {0, 3, 6}; 

然後移動比賽變得容易很多。當你想要回合時,你也可以輕鬆地完成。例如,如果你想在第二輪的第二場比賽,你可以做

races[roundStartIndicies[1] + 1] 

有時候看着一維方式2D問題有助於使代碼更乾淨。

編輯:它取決於你如何訪問數據,但你可能想要Races建立一個LinkedList作爲HuStmpHrrr建議。然而,你失去了持續的隨機訪問比賽。