2014-09-18 70 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建议。然而,你失去了持续的随机访问比赛。