【发布时间】:2019-04-07 19:51:43
【问题描述】:
我在 Java 中创建了一个函数来查找所有可能的列表组合。我只为 2 个技能创建了此代码,但由于技能的数量可能会动态变化,我需要更改我的代码以支持动态数量的嵌套循环,以找到技能的专家组合。
public List<ProjectAndTeam> teamCombinations(List<ProjectAndTeam> projectAndTeams) {
List<ProjectAndTeam> allTeamCombinations = new ArrayList<>();
for (ProjectAndTeam currentProjectTeam : projectAndTeams) {
ProjectAndTeam projectAndTeam = new ProjectAndTeam();
projectAndTeam.project = currentProjectTeam.project;
for (int i = 0; i < currentProjectTeam.expertForSkill.get(0).expertList.size(); i++) {
for (int j = 0; j < currentProjectTeam.expertForSkill.get(1).expertList.size(); j++) {
ExpertForSkill expertForSkill = new ExpertForSkill();
expertForSkill.skill = currentProjectTeam.expertForSkill.get(0).skill;
expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(0).expertList.get(i));
ExpertForSkill expertForSkillSecond = new ExpertForSkill();
expertForSkillSecond.skill = currentProjectTeam.expertForSkill.get(1).skill;
expertForSkill.expertList.add(currentProjectTeam.expertForSkill.get(1).expertList.get(j));
projectAndTeam.expertForSkill.add(expertForSkill);
projectAndTeam.expertForSkill.add(expertForSkillSecond);
}
}
allTeamCombinations.add(projectAndTeam);
}
return allTeamCombinations;
}
这是我的 ProjectAndTeam、ExprtForSkill 和 Expert 类
public class ProjectAndTeam {
int id;
Project project;
List<ExpertForSkill> expertForSkill = new ArrayList<>();
double totalSalary;
double totalProductivity;
}
public class ExpertForSkill {
String skill;
List<Expert> expertList = new ArrayList<>();
}
public class Expert {
int id;
List<String> skills = new ArrayList<>();
int capacity;
double productivity;
double salary;
}
如何获得具有不同数量嵌套循环的所有组合?
我相信我必须编写一个递归函数来处理它,但我很困惑。
【问题讨论】:
-
我强烈建议您将代码缩减为一个最小的示例。您在这里的内容有点难以理解,并且在展示您的问题方面确实没有必要。