Java 为什么我的关键和价值观相结合?
我已经编写了与stars中的指令相对应的代码Java 为什么我的关键和价值观相结合?,java,arraylist,data-structures,hashmap,Java,Arraylist,Data Structures,Hashmap,我已经编写了与stars中的指令相对应的代码 // 2 points static HashMap<String, Integer> Q1(ArrayList<String> inputList){ **// Return a HashMap containing all the input Strings as keys with values equal to the length of each String** Str
// 2 points
static HashMap<String, Integer> Q1(ArrayList<String> inputList){
**// Return a HashMap containing all the input
Strings as keys with values equal to the length of each String**
String result = "";
for(String ch : inputList){
result = result + ch;
}
String strings = result;
int number = strings.length();
HashMap<String, Integer> x = new HashMap<String, Integer>();
x.put(strings,number);
return x;
正如您所看到的,出于某种原因,我的代码结合了字符串键,并且实际上添加了值。有办法将它们分开吗?
出于某种原因,我的代码将两个字符串键结合在一起
-您编写的代码正是这样做的,为什么您会感到惊讶?如果要将每个字符串作为单独的键放置,请不要将它们串联起来,并将put
语句移动到循环中。您的代码将输入中的所有字符串串联成一个长字符串,然后按长度将其推送到映射。只需看到x.put(…)
在循环之外。。。。
Incorrect on input: [challenge accepted, testing...]
Expected output : {challenge accepted=18, testing...=10}
Your output : {challenge acceptedtesting...=28}