Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向二维数组中的下一个空索引插入元素的最有效方法是什么?_Java_Multidimensional Array - Fatal编程技术网

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

Java 向二维数组中的下一个空索引插入元素的最有效方法是什么?,java,multidimensional-array,Java,Multidimensional Array,我正在寻找一种不太复杂的方法(如果可能的话),将元素插入到二维数组的下一个空垂直索引中,同时在水平索引中随机循环 例如: 制作一张表格,显示8支球队的32名球员随机分为4轮。因此,我们希望在每一轮中安排8名球员 考虑以下内容:roundsTable[x][y]是表格,而x代表每个玩家注册的回合,而y代表每个回合中的玩家。玩家将从另一个二维数组玩家[t][p]中收集,并按索引顺序t(0,1,2,3,4,5,6,7)p(0,1,2,3)收集。但是,它们每次都将以随机轮x的形式存储在roundsTab

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

例如:

制作一张表格,显示8支球队的32名球员随机分为4轮。因此,我们希望在每一轮中安排8名球员

考虑以下内容:
roundsTable[x][y]
是表格,而
x
代表每个玩家注册的回合,而
y
代表每个回合中的玩家。玩家将从另一个二维数组
玩家[t][p]
中收集,并按索引顺序t(0,1,2,3,4,5,6,7)p(0,1,2,3)收集。但是,它们每次都将以随机轮
x
的形式存储在
roundsTable[][]
中。问题是,我如何将每个玩家放置在每个随机选择的
x
中的
y
的下一个空白位置

//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++];
        }
    }
}
//PlayerRoundOrder[]长32,包含1-4范围内的随机轮。它看起来像:{1,1,1,1,1,1,1,2,2,2,2,2,3,3,3…}然后我把它洗成这样:{4,1,1,3,2,3,4…}。然后,我取该数组的第一个索引,并将其设置为第一队球员的第一个索引的回合,然后将第二个索引设置为第一队球员的第二个索引…等等
私有void示例(int[]playersRoundOrder){
String[]roundsTable=new String[4][8];//我将用于根据每个玩家的回合存储每个玩家的表。因此,回合为3的玩家将存储在roundsTable[3][*这里是我需要解决方案的地方。我想在下一个可用位置插入第三轮中的每个玩家,等等。*]
int t=0;//计算每个队的指数。因此,只有当我为第一队的每个球员设置一个随机回合时,指数才会增加。
int p=0;//计算t中每个游戏者的索引。因此,在每1个循环后,它将增加,当t增加时,它将返回到零。
对于(int i=0;i
如果我正确理解了你的问题,你有以下球员姓名:

String players[8][4];  // first dim team, second dim player number
您希望在下面填写每轮中所有玩家的姓名:

String roundsTable[4][8]; // first dim round, second dim participant in round
以这样的方式

  • 每个玩家出现一次
  • 每队每轮有一名队员
  • 球员在每轮比赛中出现的顺序是随机的
  • 在这轮比赛中,分配到每个地方的队伍是随机的
  • 如果我错了,请纠正我。如果不是,那么这里有一个潜在的解决方案:

    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;
    }
    
    String[]]generateRoundsTable(String[]]players){
    String[][]roundsTable=新字符串[ROUND_COUNT][TEAM_COUNT];
    列表[]分配=新列表[四舍五入计数];
    对于(int r=0;r
    你能提供你想要的东西的视觉效果吗?@MarquisBlount我添加了一个视觉近似值。对不起,我应该更具体一点,我指的是你想要构建的矩阵的视觉效果。你所说的“在水平索引中随机循环”是什么意思。我有点困惑你是如何试图建立这个矩阵。您是否试图在每列中存储0-3(或1-4)之间的随机数?@MarquisBlount对造成的混乱表示抱歉。我找不到更好的方法来解释这个想法。我会尽量改变我的问题,让它更明显一点。另外,关于您的问题,我正在尝试存储在
    playersRoundOrder
    array中重复8次的数字1-4。所以,它看起来像{1,1,1,1,1,1,1,1,2,2,2,2,2…}然后它被洗牌以从中提取随机数(1-4)。我建议在你的问题中创建一个格式化的示例矩阵。“一幅画抵得上千言万语”太好了,我感谢你的回答。但不幸的是,这不是我想要的。我正在寻找一种只使用简单数组来完成这项工作的方法。我会尽量更好地解释我的想法,以避免混淆(这个想法很简单,但是没有画很难解释!!)。