将元素插入二维数组中下一个空索引的最有效方法是什么?

时间:2015-09-24 01:56:26

标签: java multidimensional-array

我正在寻找一种不那么复杂的方法(如果可能的话)将元素插入到二维数组的下一个空垂直索引中,同时随机循环遍历水平索引。

例如:

制作一张表格,显示来自8支球队的32名球员随机分配到4轮。所以,我们希望每轮投入8名球员。

考虑以下因素:roundsTable[x][y]是表格,而x表示每位玩家注册的轮次,而y代表每轮中的玩家。玩家将从另一个二维数组players[t][p]收集,他们将按索引顺序t(0,1,2,3,4,5,6,7)p(0,1,2,3)收集)。但是,它们每次都会在roundsTable[][]中随机存储x。问题是,如何将每位玩家放入已选中的每个随机yx的下一个空位。

//playersRoundOrder[] is 32 long and contains the random rounds in a range of 1-4. It looks something like that: {1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3...} then I shuffle it to become something like this: {4,1,1,3,2,3,4...}. Then, I take the first index of that array and I set it as the round for the first index of player in the first team, then the second index to the second player of the first team...etc 
private void example(int[] playersRoundOrder) { 
    String[][] roundsTable = new String[4][8]; //The table that I will use to store each player based on their round. So, the player whose round is 3, will be stored at roundsTable[3][*Here is where I need the solution. I want to insert each player in round three in the next available spot etc.*]
    int t = 0; //count the index of each team. So, it will only increase when I set a random round for each player in the first team.
    int p = 0; //count the index of each player in t. So, it will increase after each 1 loop, and it returns to zero when t increase.

    for (int i = 0; i < playersRoundOrder.length; i++) {// loop 32 times
        //String players[][] is the other two dimensional array that contains 32 players
        //(8 teams - 4 players for each)
        if ((p + 1) == players[t].length) { //If there is no more players in this team t, go to the next team and start from the index of the first player
            roundsTable[playersRoundOrder[i]] [**PROBLEM**] = players[++t][p = 0];
        } else { //If there is still players on the team, go to the next player
          roundsTable[playersRoundOrder[i]] [**PROBLEM**] = players[t][p++];
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你有以下持有玩家&#39;名称:

String players[8][4];  // first dim team, second dim player number

并且您希望在每轮中为所有玩家填写以下名称:

String roundsTable[4][8]; // first dim round, second dim participant in round

以这种方式

  1. 每位玩家出现一次
  2. 每支球队每轮都有一名球员
  3. 玩家在每轮中出现的顺序是随机的
  4. 分配到该回合中每个地方的团队是随机的
  5. 如果我错了,请纠正我。如果没有,那么这是一个潜在的解决方案:

    String[][] generateRoundsTable(String[][] players) {
        String[][] roundsTable = new String[ROUND_COUNT][TEAM_COUNT];
        List<Integer>[] assignment = new List<>[ROUND_COUNT];
        for (int r = 0; r < ROUND_COUNT; r++) {
            assignment[r] = IntStream.range(0, TEAM_COUNT).collect(Collectors.toList());
           Collections.shuffle(assignment[r]);
        }
        for (int t = 0; t < TEAM_COUNT; t++) {
            List<String> team = Arrays.asList(players[t]);
            Collections.shuffle(team);
            for (int r = 0; r < ROUND_COUNT; r++) {
                roundsTable[r][assignment[r].get(t)] = team.get(r);
            }
        }
        return roundsTable;
    }
    
相关问题