MTT
MTT

Reputation: 5263

HashSet compilation error

The only thing I want to do is putting an array (temp_X) into a HashSet, but I got the error for the HashSet: no suitable constructor found for 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));
        }

    }

Any idea how I can fix it?

Upvotes: 3

Views: 3238

Answers (2)

Reimeus
Reimeus

Reputation: 159754

Arrays#asList accepts a type array which means all elements used need to be Object types rather than primitives.

Use an Integer array instead:

Integer[] temp_X;

This will allow Arrays#asList to be used to against the wrapper class:

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

Upvotes: 3

BlackJoker
BlackJoker

Reputation: 3191

in Arrays.asList(temp_X); temp_X must be a Object array,not primitive type array. And HashSet<T> does not support primitive type .You have to convert each int in temp_X to Integer and add to temp_xList one by one

Upvotes: 1

Related Questions