java - HashSet 编译错误

标签 java compiler-errors hashset

我唯一想做的就是将数组 (temp_X) 放入 HashSet,但我得到了 HashSet 的错误:没有找到适合 HashSet(List) 的构造函数

 public PSResidualReduction(int Xdisc[][], double[][] pat_cand, int k) {

        for (int i = 0; i < Xdisc.length; i++) {
            int[] temp_X;
            temp_X = new int[Xdisc[0].length];
            for (int s = 0; s < Xdisc[0].length; s++) {
                temp_X[s] = Xdisc[i][s];
            }
            HashSet<Integer> temp_XList = new HashSet<Integer>(Arrays.asList(temp_X));
        }

    }

知道如何修复它吗?

最佳答案

Arrays#asList接受类型数组,这意味着使用的所有元素都必须是 Object 类型而不是原始类型。

改用Integer数组:

Integer[] temp_X;

这将允许使用 Arrays#asList 来对抗包装类:

HashSet<Integer> temp_XList = new HashSet<Integer>(Arrays.asList(temp_X));

关于java - HashSet 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16096412/

相关文章:

java - 什么是 ASCII 艺术文档的流行 JavaDoc 实践?

compiler-errors - Visual Studio 2013无法打开共享项目

java - 无法让 java 使用 PrintWriter 写入文本文件

java - 在 websphere 错误中连接到 jndi

c++ - 看起来很好的C++代码会导致错误

java - 如何使用 Struts 嵌套标签库从 HashSet 字段属性中获取项目?

c++ - 如何让std::hash_set<pair<T1, T2>>编译运行

c# - 使用什么算法来检查一组是否与另一组重叠?

java - 在 Java 中用于价格格式化的 DecimalFormat

Java Swing : Why drag and drop in JFrame will trigger the "Ctrl+C" accelerator?