In the following examples:
class ZiggyTest2{
public static void main(String[] args){
int[] a = { 1, 2, 3, 4,7};
List<Integer> li2 = new ArrayList<Integer>();
li2 = Arrays.asList(a);
}
}
The compiler complains that that int[] and java.lang.Integer are not compatible. i.e.
found : java.util.List<int[]>
required: java.util.List<java.lang.Integer>
li2 = Arrays.asList(a);
^
It works fine if i change the List definition to remove the generic types.
List li2 = new ArrayList();
- Shouldn't the compiler have auto-boxed the ints to Integer?
- How can i create a
List<Integer>
object from an array of ints using
Arrays.asList()?
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…