Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.asList() of an array

What is wrong with this conversion?

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

I made this after reading the solution found in Create ArrayList from array. The second line (sorting) in getTheNumber(...) causes the following exception:

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

What is wrong here? I do realize that sorting could be done with Arrays.sort(), I'm just curious about this one.

like image 274
nbl Avatar asked Aug 08 '09 12:08

nbl


People also ask

Does array copy asList?

asList method. Using this method, we can convert from an array to a fixed-size List object. This List is just a wrapper that makes the array available as a list. No data is copied or created.

What arrays return asList?

asList returns a fixed-size list that is​ backed by the specified array; the returned list is serializable and allows random access.

How is array asList () different?

1) ArrayList internally uses a dynamic array to store the elements. LinkedList internally uses a doubly linked list to store the elements. 2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory.

Can we modify arrays asList?

Arrays. asList() method returns a fixed-size list backed by the specified array. Since an array cannot be structurally modified, it is impossible to add elements to the list or remove elements from it. The list will throw an UnsupportedOperationException if any resize operation is performed on it.


1 Answers

Let's consider the following simplified example:

public class Example {     public static void main(String[] args) {         int[] factors = {1, 2, 3};         ArrayList<Integer> f = new ArrayList(Arrays.asList(factors));         System.out.println(f);     } } 

At the println line this prints something like "[[I@190d11]" which means that you have actually constructed an ArrayList that contains int arrays.

Your IDE and compiler should warn about unchecked assignments in that code. You should always use new ArrayList<Integer>() or new ArrayList<>() instead of new ArrayList(). If you had used it, there would have been a compile error because of trying to pass List<int[]> to the constructor.

There is no autoboxing from int[] to Integer[], and anyways autoboxing is only syntactic sugar in the compiler, so in this case you need to do the array copy manually:

public static int getTheNumber(int[] factors) {     List<Integer> f = new ArrayList<Integer>();     for (int factor : factors) {         f.add(factor); // after autoboxing the same as: f.add(Integer.valueOf(factor));     }     Collections.sort(f);     return f.get(0) * f.get(f.size() - 1); } 
like image 134
Esko Luontola Avatar answered Oct 04 '22 22:10

Esko Luontola