Create ArrayList from array

后端 未结 30 1813
遇见更好的自我
遇见更好的自我 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 23:02

    as all said this will do so

     new ArrayList<>(Arrays.asList("1","2","3","4"));
    

    and the common newest way to create array is observableArrays

    ObservableList: A list that allows listeners to track changes when they occur.

    for Java SE you can try

    FXCollections.observableArrayList(new Element(1), new Element(2), new Element(3));
    

    that is according to Oracle Docs

    observableArrayList() Creates a new empty observable list that is backed by an arraylist. observableArrayList(E... items) Creates a new observable array list with items added to it.

    Update Java 9

    also in Java 9 it's a little bit easy:

    List list = List.of("element 1", "element 2", "element 3");
    

提交回复
热议问题