如何完成这个关于集合的java程序
想象一下,你正在开发一款口袋妖怪游戏,你需要实现战斗团队机制。回想一下,玩家的团队最多可以容纳6个口袋妖怪。为了简化事情,您希望将其实现为一个包含6个元素的ArrayList(团队中每个口袋妖怪1个元素),为了进一步简化事情,您决定避免创建自定义的口袋妖怪类。相反,您选择将每个口袋妖怪表示为一个带有两个键的HashMap:“Name”和“Level”。与键“Name”关联的值将是一个表示口袋妖怪名称的字符串,与键“Level”关联的值将是一个表示口袋妖怪级别的整数
任务:编写一个名为createParty的公共静态方法,该方法有一个String[]类型的参数,名为包含Pokemon名称的names,后跟一个int[]类型的参数,名为包含Pokemon级别的levels(其中names[i]和levels[i]是参与方中Pokemon i的名称和级别)。它应该将该党作为ArrayList返回>;如上所述
Sample Input:
Pikachu Venasaur Charizard Blastoise Lapras Snorlax
88 84 84 84 80 82
Sample Output:
Pikachu 88
Venasaur 84
Charizard 84
Blastoise 84
Lapras 80
Snorlax 82
我的代码如下,但它提醒我一个错误
public static ArrayList<HashMap<String, Object>> createParty(String[] names,int[] levels) {
ArrayList<HashMap<String, Object>> party = new ArrayList<HashMap<String,Object>>(6);
for(int i=0;i<6;i++) {
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put(names[i], levels[i]);
party.add(hm);
}
return party;
}
错误如下所示
Failed test #1. The 'ArrayList' your 'createParty' method returned contained a 'HashMap' that was missing the "Name" key
Input:
Pikachu Venasaur Charizard Blastoise Lapras Snorlax
88 84 84 84 80 82
Your output:
MISSING_NAME
Correct output:
Pikachu 88
Venasaur 84
Charizard 84
Blastoise 84
Lapras 80
Snorlax 82
你能帮我找出哪部分错了吗, 非常感谢。:)
# 1 楼答案
它似乎是在期待,而不是
使用固定键返回名称和级别: