//类型转换
// 接收long[]类型的字符串数组,转换成string类型的字符串数组
String[] strArr = new String[userIds.length];
for(int i=0;i<strArr.length;i++){
strArr[i] = String.valueOf(userIds[i]);
}
// 将字符串数组转为List<Long> 类型
List<Long> LString = new ArrayList<Long>();
for(String str : strArr){
LString.add(new Long(str));
}
list添加元素时抛出 java.lang.UnsupportedOperationException :null
将数组转为list时,用到了Arrays.asList(),然后使用add方法,抛出了异常。
报错原因:返回的list其实是AbstractList。该List内部使用final修饰的一个数组来实现的,并没有实现list的remove()和add()方法,只是简单的抛异常。相当于是只读的,并不支持增删操作。
解决方法:将该列表拷贝到一个新的Arraylist中,创建一个新的ArrayList对象
List<Long> deptIds = new ArrayList<>();
//数组转化成list
//用到Arrays.asList(),deptIds不能使用remove()和add()
deptIds = Arrays.asList(user.getDeptIds());
Long deptIdParam = 0;
List<Long> newdeptIds = new ArrayList<>(deptIds);
newdeptIds.add(deptIdParam);
deptIds=newdeptIds;