Create ArrayList from array

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

    Since Java 8 there is an easier way to transform:

    import java.util.List;    
    import static java.util.stream.Collectors.toList;
    
    public static  List fromArray(T[] array) {
        return Arrays.stream(array).collect(toList());
    }
    

提交回复
热议问题