Arrays.asList() of an array Arrays.asList()方法用于将数组转换为List。

5 浏览
0 Comments

Arrays.asList() of an array Arrays.asList()方法用于将数组转换为List。

这个转换有什么问题?

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

在阅读Create ArrayList from array的解决方案后,我写了这个代码。在getTheNumber(...)的第二行(排序)会导致以下异常:

Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to java.lang.Comparable]

这里有什么问题?我意识到可以用Arrays.sort()来进行排序,只是对这个问题感到好奇。

0