The following code doesn’t work though it seems quite correct:
import java.util.*;
public class Jaba {
public static void main(String args[]) {
Random rand = new Random();
int[] array = new int[10];
for (int i = 0; i < array.length; ++i) {
array[i] = rand.nextInt(30);
}
Queue<Integer> que = new PriorityQueue<Integer>();
Collections.addAll(que, Arrays.asList(array));
}
}
What should be fixed?
Arrays.asListtakes an array of objects. (It’s the only option, as it can’t return aList<int>.)When you pass an
int[]toArrays.asList, it will be interpreted as a singleton array containing oneint[].You’ll have to
Change to a for-loop:
or
change the type of
arrayfromint[]toInteger[].This would allow you to do
or,
The reason that
Collections.addAll(que, Arrays.asList(array))fails is that it takes aT...as second argument, which is really an array and not a List.