一、集合转换成数组(toArray)
(1)不指定转化类型(object类)
1 ArrayList<String> al=new ArrayList<String>();
2 al.add("hello");
3 al.add("world");
4 Object[] obj=al.toArray();
5 for (Object object : obj) {
6 System.out.println(object);
7 }
(2)指定转换类型
1 ArrayList<String> al=new ArrayList<String>();
2 al.add("hello");
3 al.add("world");
4 String []strArray=new String[1];//new String[5];
5 strArray=al.toArray(strArray);
6 for (String str : strArray) {
7 System.out.println(str);
8 }
9
二、数组转换成集合(asList)
1 int [] array={11,22,33};
2 List list=Arrays.asList(array); //[[I@1540e19d]
3 System.out.println(list);
4
5 Integer[] array2=new Integer[]{11,22,33}; //注意要转换成包装类
6 List<Integer> list2=Arrays.asList(array2); //[11, 22, 33]
7 System.out.println(list2);