做题时遇到的一个方法
final List<Student> students = Arrays.asList(
new Student("Cloudy",1,PaperType.Thesis),
new Student("Windy",2,PaperType.Any)
);
其中Student为自定义学生对象,为immutable类
其属性为:
private final String name; //学生姓名
private final int preferredQuantity; //希望选择的论文数目
private final PaperType preferredType; //偏好的论文类型
构造方法为:
public Student(String name,int quantity,PaperType type){
this.name = name;
this.preferredQuantity = quantity;
this.preferredType = type;
}
Snapshot Diagram 为:
Arrays.asList方法:
将数组转化为List集合。
如:List list = Arrays.asList(“a”,“b”,“c”);
注意:
1 该方法适用于对象型数据的数组
2 该方法将数组与List列表链接起来:当更新其一个时,另一个自动更新(List无法改变)
3 该方法产生的List为unmodifiable类型,不支持add(),remove()等操作。
4 最好不要适配int等基本数据类型
如下图:
public static void main(String[] args) {
int a[] = {1,2};
Integer b[] = {1,2};
System.out.println(Arrays.asList(a));
List<Integer> list1 = Arrays.asList(b);
System.out.println(list1);
}
结果为