Arrays.asList() of an array

后端 未结 9 806
我寻月下人不归
我寻月下人不归 2020-11-27 15:41

What is wrong with this conversion?

public int getTheNumber(int[] factors) {
    ArrayList f = new ArrayList(Arrays.asList(factors));  
    Co         


        
9条回答
  •  情歌与酒
    2020-11-27 16:22

    You are trying to cast int[] to Integer[], this is not possible.

    You can use commons-lang's ArrayUtils to convert the ints to Integers before getting the List from the array:

    public int getTheNumber(int[] factors) {
        Integer[] integers = ArrayUtils.toObject(factors);
        ArrayList f = new ArrayList(Arrays.asList(integers));
        Collections.sort(f);
        return f.get(0)*f.get(f.size()-1);
    }    
    

提交回复
热议问题