list和陣列的相互轉換
阿新 • • 發佈:2019-01-07
1 list轉陣列
// list轉陣列
List<String> strList = new ArrayList<String>();
strList.add("aaa");
strList.add("bbb");
strList.add("ccc");
String[] strArray = strList.toArray(new String[strList.size()]); System.out.println(Arrays.toString(strArray));
2 陣列轉list(Arrays.asList和Collections.addAll兩種方法)
// 陣列轉list List<String> strList = Arrays.asList("111","222","333"); System.out.println(strList); String[] strArray = {"111","222","333"}; List<String> sList = new ArrayList<String>(); Collections.addAll(sList, strArray); System.out.println(sList);