Arrays.asList() of an array

后端 未结 9 789
我寻月下人不归
我寻月下人不归 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:07

    Arrays.asList(factors) returns a List, not a List. Since you're doing new ArrayList instead of new ArrayList you don't get a compile error for that, but create an ArrayList which contains an int[] and you then implicitly cast that arraylist to ArrayList. Of course the first time you try to use one of those "Integers" you get an exception.

    提交回复
    热议问题