把下面这段js代码转换成java代码/** * * @param {array} peopleList 选手列表 * @param {number} round_num 每人打的场数,默认选手人数-1 */ function getTempMatchList(peopleList,round_num) { let p_num = peopleList.length; let partner_group_list = []; //所有有可能搭档的组 let partner_group_list_real = []; //出场的搭档数组 let p_should_round_num = {}; //记录每人上场的次数对象 let ground_num = 0; for (let i = 0; i < peopleList.length; i++) { p_should_round_num[peopleList[i]] = peopleList.length-1; //预先定下前面的搭档池数 } if((p_num*round_num)%4==0){ //整除 ground_num = Math.floor(peopleList.length*round_num/2); for (let i = 0; i < peopleList.length; i++) { const e = peopleList[i]; for (let j = i+1; j < peopleList.length; j++) { const e1 = peopleList[j]; partner_group_list.push([e,e1]); //准确的搭档池 } } partner_group_list_real = [...partner_group_list]; if(round_num>peopleList.length-1){ //必须每人多打几场 let temp_partner_group_list = [...partner_group_list]; let flag = true; while (flag) { //随机拿一组出来 let index = Math.floor(Math.random()*temp_partner_group_list.length); let partner_group_list_real_one = temp_partner_group_list.splice(index,1); let p1 = partner_group_list_real_one[0][0]; let p2 = partner_group_list_real_one[0][1]; if(p_should_round_num[p1]<round_num&&p_should_round_num[p2]<round_num){ partner_group_list_real.push(partner_group_list_real_one[0]); p_should_round_num[p1] +=1; p_should_round_num[p2] +=1; } if(partner_group_list_real.length==ground_num){ flag = false; } } } return partner_group_list_real; }else{ return []; } }
时间: 2024-02-17 20:00:34 浏览: 80
以下是将 JavaScript 代码转换为 Java 代码的尝试:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
public class Main {
public static void main(String[] args) {
List<String> peopleList = new ArrayList<>();
peopleList.add("A");
peopleList.add("B");
peopleList.add("C");
peopleList.add("D");
int round_num = 3;
List<List<String>> partner_group_list_real = getTempMatchList(peopleList, round_num);
System.out.println(partner_group_list_real);
}
public static List<List<String>> getTempMatchList(List<String> peopleList, int round_num) {
int p_num = peopleList.size();
List<List<String>> partner_group_list = new ArrayList<>(); // 所有有可能搭档的组
List<List<String>> partner_group_list_real = new ArrayList<>(); // 出场的搭档数组
Map<String, Integer> p_should_round_num = new HashMap<>(); // 记录每人上场的次数对象
int ground_num = 0;
for (int i = 0; i < peopleList.size(); i++) {
p_should_round_num.put(peopleList.get(i), peopleList.size() - 1); // 预先定下前面的搭档池数
}
if ((p_num * round_num) % 4 == 0) { // 整除
ground_num = (int) Math.floor(peopleList.size() * round_num / 2);
for (int i = 0; i < peopleList.size(); i++) {
String e = peopleList.get(i);
for (int j = i + 1; j < peopleList.size(); j++) {
String e1 = peopleList.get(j);
List<String> partner_group = new ArrayList<>();
partner_group.add(e);
partner_group.add(e1);
partner_group_list.add(partner_group); // 准确的搭档池
}
}
partner_group_list_real.addAll(partner_group_list);
if (round_num > peopleList.size() - 1) { // 必须每人多打几场
List<List<String>> temp_partner_group_list = new ArrayList<>(partner_group_list);
boolean flag = true;
Random rand = new Random();
while (flag) {
// 随机拿一组出来
int index = rand.nextInt(temp_partner_group_list.size());
List<String> partner_group_list_real_one = temp_partner_group_list.remove(index);
String p1 = partner_group_list_real_one.get(0);
String p2 = partner_group_list_real_one.get(1);
if (p_should_round_num.get(p1) < round_num && p_should_round_num.get(p2) < round_num) {
partner_group_list_real.add(partner_group_list_real_one);
p_should_round_num.put(p1, p_should_round_num.get(p1) + 1);
p_should_round_num.put(p2, p_should_round_num.get(p2) + 1);
}
if (partner_group_list_real.size() == ground_num) {
flag = false;
}
}
}
return partner_group_list_real;
} else {
return new ArrayList<>();
}
}
}
```
需要注意的是,在 Java 中,数组是固定长度的,而 List 是可变长度的,所以需要使用 List 来替代数组,并且需要使用泛型来指定 List 中存储的数据类型。此外,Java 中的 Map 是类似于 JavaScript 中的对象的数据结构,也需要注意使用。
阅读全文