List和ArrayList之间转换的例子
package Test01; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test01 { public static void main(String[] args){ a(); b(); c(); } @SuppressWarnings("unchecked") public static void a(){ @SuppressWarnings("rawtypes") List list = new ArrayList(); list.add(0); list.add(1); list.add(3); list.add(4); System.out.println(list); System.out.println(list.getClass()); } public static void b(){ Double []arr=new Double[]{1.0,2.0,3.0,null}; @SuppressWarnings("rawtypes") List list = Arrays.asList(arr); System.out.println(list); System.out.println(list.getClass()); } @SuppressWarnings("unchecked") public static void c(){ Double []arr=new Double[]{1.0,2.0,3.0,null}; @SuppressWarnings("rawtypes") List list = Arrays.asList(arr); @SuppressWarnings("rawtypes") ArrayList arrayList = new ArrayList(); for (int i = 0; i < list.size(); i++) { arrayList.add(list.get(i)); } System.out.println(arrayList); System.out.println(arrayList.getClass()); } }
代码运行后控制台输出结果如下: