Arrays.asList() of an array
Solution 1
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);
}
Solution 2
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<Integer> f = new ArrayList<Integer>(Arrays.asList(integers));
Collections.sort(f);
return f.get(0)*f.get(f.size()-1);
}
Solution 3
there are two cause of this exception:
1
Arrays.asList(factors)
returns a List<int[]>
where factors
is an int array
2
you forgot to add the type parameter to:
ArrayList<Integer> f = new ArrayList(Arrays.asList(factors));
with:
ArrayList<Integer> f = new ArrayList<Integer>(Arrays.asList(factors));
resulting in a compile-time error:
found : java.util.List<int[]> required: java.util.List<java.lang.Integer>
Solution 4
Use java.utils.Arrays:
public int getTheNumber(int[] factors) {
int[] f = (int[])factors.clone();
Arrays.sort(f);
return f[0]*f[(f.length-1];
}
Or if you want to be efficient avoid all the object allocation just actually do the work:
public static int getTheNumber(int[] array) {
if (array.length == 0)
throw new IllegalArgumentException();
int min = array[0];
int max = array[0];
for (int i = 1; i< array.length;++i) {
int v = array[i];
if (v < min) {
min = v;
} else if (v > max) {
max = v;
}
}
return min * max;
}
Solution 5
Arrays.asList(factors)
returns a List<int[]>
, not a List<Integer>
. Since you're doing new ArrayList
instead of new ArrayList<Integer>
you don't get a compile error for that, but create an ArrayList<Object>
which contains an int[]
and you then implicitly cast that arraylist to ArrayList<Integer>
. Of course the first time you try to use one of those "Integers" you get an exception.
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on July 29, 2020Comments
-
Admin almost 4 years
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. -
MaheshVarma over 10 yearsdoes the constructor ArrayList(List<int[]>) exist? @dfa
-
Matthias over 10 years+1, because this solution does fit the problem better than the unhandy Collections.
-
Ilmari Karonen almost 9 yearsYour image link seems to have broken. If you still have the original image, please reupload it to stack.imgur, or just edit your answer to make it work without the image. Thanks.
-
Esko Luontola almost 9 yearsThanks for the notice. The current version of IntelliJ IDEA gave a different kind of warning than I recall the broken image to have contained (IIRC there used to be an
Arrays.asList
specific warning), so I edited the answer to work without the image. -
klaar over 8 years@MaheshVarma Yes, why not? But then you need to specify that very type in the ArrayList as well:
ArrayList<int[]> f = new ArrayList<int[]>(Arrays.asList(factors));
. Always remember thatint[]
is just a subclass ofObject
, so it's allowed as a generic type.