将String数组转换为java.util.List


Answers:


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

这是数组的列表视图,该列表部分不可修改,您无法添加或删除元素。但是时间复杂度是O(1)。

如果您想要一个可修改的列表:

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

这会将源数组中的所有元素复制到新列表中(复杂度:O(n))


27

使用static,List list = Arrays.asList(stringArray)或者您可以遍历数组并将字符串添加到列表中。


20
import java.util.Collections;

List myList = new ArrayList();
String[] myArray = new String[] {"Java", "Util", "List"};

Collections.addAll(myList, myArray);

10

最简单的方法:

String[] stringArray = {"Hey", "Hi", "Hello"};

List<String> list = Arrays.asList(stringArray);

7

第一步,您需要通过Arrays.asList()创建一个列表实例。

String[] args = new String[]{"one","two","three"};
List<String> list = Arrays.asList(args);//it converts to immutable list

然后,您需要将“列表”实例传递给 new ArrayList();

List<String> newList=new ArrayList<>(list);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.