Array to Collection: Optimized code

前端 未结 10 2028
萌比男神i
萌比男神i 2021-01-31 14:26

Is there a better way of achieving this?

public static List toList(String[] array) {

    List list = new ArrayList(array.length);

          


        
10条回答
  •  故里飘歌
    2021-01-31 14:46

    Yes, there is. You can use the Arrays class from the java.util.* package. Then it's actually just one line of code.

    List list = Arrays.asList(array);
    

提交回复
热议问题