Converting String array to java.util.List

前端 未结 6 1833
孤街浪徒
孤街浪徒 2020-12-07 11:30

How do I convert a String array to a java.util.List?

6条回答
  •  臣服心动
    2020-12-07 12:17

    List strings = Arrays.asList(new String[]{"one", "two", "three"});
    

    This is a list view of the array, the list is partly unmodifiable, you can't add or delete elements. But the time complexity is O(1).

    If you want a modifiable a List:

    List strings = 
         new ArrayList(Arrays.asList(new String[]{"one", "two", "three"}));
    

    This will copy all elements from the source array into a new list (complexity: O(n))

提交回复
热议问题