Create ArrayList from array

后端 未结 30 2276
遇见更好的自我
遇见更好的自我 2020-11-21 22:29

I have an array that is initialized like:

Element[] array = {new Element(1), new Element(2), new Element(3)};

I would like to convert this

30条回答
  •  萌比男神i
    2020-11-21 23:11

    Since this question is pretty old, it surprises me that nobody suggested the simplest form yet:

    List arraylist = Arrays.asList(new Element(1), new Element(2), new Element(3));
    

    As of Java 5, Arrays.asList() takes a varargs parameter and you don't have to construct the array explicitly.

提交回复
热议问题