【发布时间】:2020-03-28 07:59:08
【问题描述】:
如果列表已存在,我将获取该列表并向其添加元素。
代码 sn-p:
Map<Integer, List<Integer>> map = new TreeMap<>(Collections.reverseOrder());
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int factor = 0;
int num = sc.nextInt();
for (int j = 1; j <= num; j++)
if (num % j == 0) factor++; //just getting factor count of each input
map.put(factor, map.getOrDefault(factor, new ArrayList<>()).add(num)); // error line
错误详情:
错误:不兼容的类型:布尔值无法转换为列表
如何解决这个问题?
【问题讨论】:
-
你打算
map.put(factor, map.getOrDefault(factor, new ArrayList<>())).add(num)吗? (右括号不匹配) -
没有括号不匹配我只是获取列表(如果已经存在)或创建一个新列表
-
"不兼容的类型:boolean 无法转换为 List" ... 因为
.add返回一个boolean值,并且您已将其作为值包含在 @ 987654325@ 操作,其中需要List作为值。
标签: java arraylist collections compiler-errors maps