首页 > 解决方案 > 没有找到适合 put(int, ArrayList) 的方法

问题描述

我有一段代码:

    public static void Sort(int N, int k, int[][] books) {
        TreeMap<Integer, List<Integer>> map = new TreeMap<>();
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            int curDisorder = 0;
            for (int j = 0; j < k; j++) {
                for (int u = j+1; u < k; u++) {
                    if (books[i][j] > books[i][u]) {
                        curDisorder++;
                    }
                }
            }
            map.put(curDisorder, new ArrayList<>(Arrays.asList(books[i])));
        }
        for (ArrayList<Integer> list: map.values) {
            res.add(list);
        }
        System.out.print(res);
    }

但是,它在 map.put() 有错误,它说:

method Map.put(Integer, List) is not applicable
argument mismatch: cannot infer type arguments for ArrayList<>
reason: inference variable E has incompatible bounds

这段代码有什么问题?

标签: java

解决方案


我认为您必须为您的 Arraylist 设置集合类型:

map.put(curDisorder, new ArrayList<Integer>(Arrays.asList(books[i])));

推荐阅读