1. 程式人生 > >數組和List 互轉

數組和List 互轉

arrays 集合

public class TestArrayList {


public static void main(String[] args) {

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

list.add("AAA");

list.add("BBB");

list.add("CCC");

System.out.println(Arrays.toString(listToArray(list)));

System.out.println();

list = arrayToList(listToArray(list)) ;

for(String l : list){

System.out.println(l);

}

}

public static String[] listToArray(List<String> list){

return (String[]) list.toArray(new String[list.size()]) ;

}

public static List<String> arrayToList(String[] array){

return Arrays.asList(array) ;

}

}


數組和List 互轉