Create ArrayList from array

后端 未结 30 1816
遇见更好的自我
遇见更好的自我 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条回答
  •  情书的邮戳
    2020-11-21 22:51

    Already everyone has provided enough good answer for your problem. Now from the all suggestions, you need to decided which will fit your requirement. There are two types of collection which you need to know. One is unmodified collection and other one collection which will allow you to modify the object later.

    So, Here I will give short example for two use cases.

    • Immutable collection creation :: When you don't want to modify the collection object after creation

      List elementList = Arrays.asList(array)

    • Mutable collection creation :: When you may want to modify the created collection object after creation.

      List elementList = new ArrayList(Arrays.asList(array));

提交回复
热议问题