java 数组与集合(list)相互转化
上代码
package com.core.array2collection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Demo { public static void main(String[] args) { List<Student> stuList = new ArrayList<Student>(); for(int i = 0 ; i < 5; i ++){ Student stu = new Student(); stuList.add(stu); stu.setId(i); stu.setName("007"+i); stu.setAge(10+i); } Student[] stuArray = stuList.toArray(new Student[stuList.size()]); System.out.println(stuArray); List<Student> stuList2 = Arrays.asList(stuArray); System.out.println(stuList2); } }
总结:
1.代码中的"Student[] stuArray = stuList.toArray(new Student[stuList.size()]);"为集合转数组
前提条件为集合stuList != null
2.代码中的"List<Student> stuList2 = Arrays.asList(stuArray);"为数组转集合
前提条件为stuArray != null