简体   繁体   English

如何根据数组列表中每个对象的属性将自定义对象的数组列表分成 x 组?

[英]How can I divide an arraylist of custom objects into groups of x based on the attributes of each object in the arraylist?

I have been trying to figure this one out, and I'm still scratching my head.我一直在想办法解决这个问题,但我还在摸不着头脑。 I need to divide an arraylist of objects (people in this case) into equal groups of 6, from a total of 12-25 people.我需要将一个对象数组列表(在本例中为人)分成相等的 6 组,总共 12-25 人。 They need to be divided into teams of 6 with both halves sorted by the best of both the AttackStat and the DefenseStat of each person object.他们需要分成 6 人一组,两半都根据每个人对象的AttackStatDefenseStat中最好的进行排序。 I have already written algorithms to sort the arraylist based on the best of each stat, but I'm not sure how to implement a way to display groups of six with no repeating names.我已经编写了算法来根据每个统计数据的最佳值对 arraylist 进行排序,但我不确定如何实现一种方法来显示没有重复名称的 6 组。

I am given the instruction:我得到指示:

Your program will need to be able to divide a roster into the maximum possible number of teams.您的程序需要能够将花名册划分为尽可能多的团队。 For example, a roster with 18 players on it would be divided up into three different scrimmage teams with 6 players each.例如,一个有 18 名球员的名单将被分成三个不同的训练队,每队有 6 名球员。 A roster file will always contain a list of between 12 and 25 players, and a scrimmage team always has 6 players.花名册文件总是包含 12 到 25 名球员的名单,而一个训练队总是有 6 名球员。

The coach would like the teams to be reasonably well balanced.教练希望球队能够相当平衡。 To do this, your program should make up half of each scrimmage team by distributing the best attackers to each of the different teams.为此,您的程序应通过将最佳攻击者分配给每个不同的团队来构成每个混战团队的一半。 For example, a roster with 12 players on it would be divided up into 2 scrimmage teams, and three of the top 6 attackers would be assigned to each of the two scrimmage teams.例如,一个有 12 名球员的名单将被分成 2 个训练队,前 6 名攻击手中的 3 名将被分配到两个训练队中的每一个。 After the top attackers have been assigned to make up half of each scrimmage team, then the top blockers (from the remaining unassigned players) would similarly be assigned (three to each scrimmage team in this example) to make up the other half of each team.在顶级攻击者被分配到每个混战队的一半之后,然后顶级拦网手(来自剩余未分配的球员)将被类似地分配(在这个例子中每个混战队三人)来组成每队的另一半.

Here's some of what I have right now in my Roster class:这是我现在在名册课程中拥有的一些内容:

    void sortbyAttack() {
    peopleArrayList.sort(Comparator.comparing(Person::getAttackStat).reversed());
}

void sortbyDefense() {
    peopleArrayList.sort(Comparator.comparing(Person::getDefenseStat).reversed());
}

void makeTeams() {
    ArrayList<String> team1 = new ArrayList<>();
    ArrayList<String> team2 = new ArrayList<>();
    ArrayList<String> team3 = new ArrayList<>();
    ArrayList<String> team4 = new ArrayList<>();

    sortbyAttack();
    for (int i = 0; i < 6; i++) {
        team1.add(peopleArrayList.get(i).toString());
    }
    for (int i = 6; i < 12; i++) {
        team2.add(peopleArrayList.get(i).toString());
    }
    for (int i = 0; i < 3; i++) {
        team3.add(team1.get(i).toString());
        team3.add(team2.get(i).toString());
    }
    for (int i = 3; i < 6; i++) {
        team4.add(team1.get(i).toString());
        team4.add(team2.get(i).toString());
    }
    System.out.println(team1 + "\n" + team2);
    System.out.println(team3 + "\n" + team4);
  }
}

I have tried to figure this out, I looked all over this site and googled it about a hundred times, but I can't seem to find a proper way to implement the sorting and dividing and distributing my custom objects by local values into the max amount of groups of six, without any repeating people on the same scrimmage team.我试图弄清楚这一点,我浏览了整个网站并用谷歌搜索了大约一百次,但我似乎无法找到一种正确的方法来实现按本地值对自定义对象进行排序划分分配到最大值六人一组,同一个混战队中没有重复的人。

First of all the condition on your loops forces the team to be one specific length, in this case 36 because I suppose you don't want duplicated players on teams.首先,循环中的条件强制团队的长度为特定长度,在本例中为 36,因为我想你不希望团队中有重复的玩家。 I'm pretty sure your professor wants a solution able to work for n players.我很确定您的教授想要一个能够为 n 个玩家工作的解决方案。

This said, I think you got to work around the / operator, the entire division.这就是说,我认为你必须围绕/运营商,整个部门工作。 Let's say you indicate n = 31. n/6 Will give a result of 5 .假设您指示 n = 31。 n/6将给出结果5 You got then the numbers of teams you have, so your method createTeams has to have a loop of 5 iterations where you create a new ArrayList .然后你得到了你拥有的团队数量,所以你的方法createTeams必须有一个 5 次迭代的循环,你可以在其中创建一个新的ArrayList I recommend having another dynamic structure to save every team, like another ArrayList .我建议使用另一个动态结构来保存每个团队,例如另一个ArrayList

Once you created your "teams", you sort your players correctly and then add them to your different teams.一旦你创建了你的“团队”,你就可以正确地对你的球员进行分类,然后将他们添加到你的不同团队中。 Here we can find the problem of duplications.在这里我们可以找到重复的问题。 Be sure to not add twice the same player by: a) Saving the hole list on an auxiliar structure where once you pick them you erase them b) Using carefully the same pointer to your initial structure representing the rooster确保不要通过以下方式添加两次相同的玩家:a) 将洞列表保存在辅助结构上,一旦你选择它们,你就会擦除它们 b) 小心地使用相同的指针指向代表公鸡的初始结构

I think you got the idea, the methods you used are correct but you got to work on the algorithm.我想你明白了,你使用的方法是正确的,但你必须研究算法。 Good luck, any further questions feel free to ask.祝你好运,如有任何其他问题,请随时提出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换ArrayList <Object> 到ArrayList <String> 或ArrayList <Timestamp> ? - How can I convert ArrayList<Object> to ArrayList<String> or ArrayList<Timestamp>? 如何比较 ArrayList 中的对象属性? - How to compare Objects attributes in an ArrayList? 如何将对象添加到对象数组列表中的数组列表中? - How do I add an object to an arraylist within an arraylist of objects? 排序由自定义对象填充的ArrayList对象,每个对象具有两个数据字段 - Sorting an ArrayList object populated with custom objects with two data fields each 如何将对象数组列表转换为哈希表,其中键和值是对象属性 - How to convert a object arraylist into a hashmap where key and value are the objects attributes 如何对对象的ArrayList进行排序? - How can I sort an ArrayList of objects? 如何转换 ArrayList<Object> 到 ArrayList<String> ? - How can I convert ArrayList<Object> to ArrayList<String>? 如何基于Java中对象的一个​​成员变量从数组列表中删除另一个数组列表中存在的对象? - How do I remove objects from an arraylist which are present in another arraylist based on one member variable of the object in Java? 如何让我的每个平铺对象都以一个空的arraylist初始化,以后可以向其中添加内容? - How can I have each of my tile objects be initiate with an empty arraylist that I can add things to later? 如何过滤自定义ArrayList - How Can I Filter Custom ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM