【发布时间】:2021-02-10 18:50:47
【问题描述】:
在这段代码中,我试图将每个字符映射到代表其出现的计数器。但是,在第 7 行 (int count = map.getOrDefault(chars[i], 0);) 中,出现了一个编译错误:
Solution.java:7:错误:找不到符号
整数计数 = map.getOrDefault(chars[i], 0);
^
符号:方法 getOrDefault(char,int)
位置:HashMap
static String isValid(String s) {
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
char[] chars = s.toCharArray();
int N = chars.length;
boolean chance = true;
for(int i = 0 ; i < N ; i++){
int count = map.getOrDefault(chars[i], 0);
if(count >= 1){
if(chance)
chance = false;
else
return "NO";
}
map.put(chars[i], count + 1);
}
return "YES";
}
【问题讨论】:
-
可能需要显式转换。
Character不是char和Integer不是int -
代码对我有用。您使用的是什么 Java 版本?
-
除了Simon说的,你确定导入了
java.util.Map而不是其他类型吗?
标签: java string dictionary compiler-errors default