数组和集合List的相互转化

一、数组转为List

调用Arrays类的静态方法asList

1   static void convertArray2List() {
2         String[] strs = new String[]{"hehe","haha","heihei"};
3         List list = Arrays.asList(strs);
4         
5         System.out.println(JSON.toJSONString(list));
6     }

结果 : ["hehe","haha","heihei"]

 

二、List转为数组(以ArrayList为例)

①、Object[] toArray();

②、T[] toArray(T[] a);

1   static void convertList2Array() {
2         List list = new ArrayList();
3         list.add("hehe");
4         list.add("haha");
5         list.add("heihei");
6         String[] strs =  list.toArray(new String[]{});
7         
8         System.out.println(JSON.toJSONString(strs));
9     }

结果 :["hehe","haha","heihei"]

 

你可能感兴趣的:(数组和集合List的相互转化)