把下面这段js代码转换为java代码 function getMatchList(peopleList,round_num) { while ((peopleList.length*round_num)%4!=0) { round_num++; } let matchListPoor = getTempMatchList(peopleList,round_num); let all_round = matchListPoor.length/2; let res = []; let rivalry = []; let round = 0; while (matchListPoor.length>0) { let per = matchListPoor[0]; matchListPoor.splice(0,1); if(rivalry.length==0){ rivalry.push(per); round = 1; }else if(!rivalry[0].includes(per[0])&&!rivalry[0].includes(per[1])){ rivalry.push(per); }else{ matchListPoor.push(per); round = 2; } if(rivalry.length==2){ res.push(rivalry); rivalry = []; } if(matchListPoor.length==1&&round == 2){ matchListPoor = [...matchListPoor,...rivalry]; for (let j = 0; j < res.length; j++) { if(!res[j][0].includes(matchListPoor[0][0])&&!res[j][0].includes(matchListPoor[0][1])){ let temp = res[j][1]; res[j][1] = matchListPoor[0]; matchListPoor[0] = temp; if(!matchListPoor[0].includes(matchListPoor[1][0])&&!matchListPoor[0].includes(matchListPoor[1][1])){ res.push(matchListPoor); break; } } } } if(res.length==all_round){ break; } } return res; }
时间: 2024-01-27 21:03:43 浏览: 81
下面是将给定的 JavaScript 代码翻译成 Java 代码的示例:
```
public static List<List<String[]>> getMatchList(List<String[]> peopleList, int round_num) {
while ((peopleList.size() * round_num) % 4 != 0) {
round_num++;
}
List<String[][]> matchListPoor = getTempMatchList(peopleList, round_num);
int all_round = matchListPoor.size() / 2;
List<List<String[]>> res = new ArrayList<>();
List<String[][]> rivalry = new ArrayList<>();
int round = 0;
while (matchListPoor.size() > 0) {
String[][] per = matchListPoor.get(0);
matchListPoor.remove(0);
if (rivalry.size() == 0) {
rivalry.add(per);
round = 1;
} else if (!contains(rivalry.get(0), per[0]) && !contains(rivalry.get(0), per[1])) {
rivalry.add(per);
} else {
matchListPoor.add(per);
round = 2;
}
if (rivalry.size() == 2) {
res.add(Arrays.asList(rivalry.get(0), rivalry.get(1)));
rivalry.clear();
}
if (matchListPoor.size() == 1 && round == 2) {
matchListPoor.addAll(rivalry);
for (int j = 0; j < res.size(); j++) {
if (!contains(res.get(j).get(0), matchListPoor.get(0)[0])
&& !contains(res.get(j).get(0), matchListPoor.get(0)[1])) {
String[][] temp = res.get(j).get(1);
res.get(j).set(1, matchListPoor.get(0));
matchListPoor.set(0, temp);
if (!contains(matchListPoor.get(0), matchListPoor.get(1)[0])
&& !contains(matchListPoor.get(0), matchListPoor.get(1)[1])) {
res.add(Arrays.asList(matchListPoor.get(0), matchListPoor.get(1)));
break;
}
}
}
}
if (res.size() == all_round) {
break;
}
}
return res;
}
private static boolean contains(String[][] arr, String str) {
return Arrays.stream(arr).flatMap(Arrays::stream).anyMatch(s -> s.equals(str));
}
private static List<String[][]> getTempMatchList(List<String[]> peopleList, int round_num) {
List<String[][]> tempMatchList = new ArrayList<>();
for (int i = 0; i < round_num; i++) {
String[][] matchList = new String[peopleList.size() / 2][2];
for (int j = 0; j < peopleList.size() / 2; j++) {
matchList[j] = new String[]{peopleList.get(j)[0], peopleList.get(peopleList.size() - 1 - j)[0]};
}
peopleList.remove(0);
peopleList.remove(peopleList.size() - 1);
tempMatchList.add(matchList);
}
return tempMatchList;
}
```
需要注意的是,Java 中没有类似 JavaScript 中的 `splice()` 方法,因此需要使用 `remove()` 方法和 `addAll()` 方法代替。另外,在 Java 中,数组不能直接作为列表的元素,因此需要使用二维字符串数组 `String[][]` 代替。
阅读全文