java list arraylist差異




將字符串數組轉換為java.util.List (3)

如何將String數組轉換為java.util.List


使用靜態List list = Arrays.asList(stringArray)或者你可以迭代數組並將字符串添加到列表中。


第一步你需要通過Arrays.asList()創建一個列表實例;

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

然後你需要將'list'實例傳遞給new ArrayList();

List<String> newList=new ArrayList<>(list);

import java.util.Collections;

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

Collections.addAll(myList, myArray);




collections