Arrays.aslist的使用

做题时遇到的一个方法

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);		
}

结果为

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值