Varargs to ArrayList problem in Java

后端 未结 4 1098
一整个雨季
一整个雨季 2020-12-05 04:20

I don\'t understand why the following does not work:

public void doSomething(int... args){
  List broken = new ArrayList(Arrays         


        
4条回答
  •  一个人的身影
    2020-12-05 04:31

    In this case, autoboxing (automatic conversion from int to Integer) doesn't work. You have to add each int manually to the list.

    If you need code like that often, consider using commons lang which has org.apache.commons.lang.ArrayUtils.toObject(int[])

提交回复
热议问题