问题的提出
Arrays.asList();
(1) 该方法对于基本数据类型的数组支持并不好,当数组是基本数据类型时不建议使用
(2) 当使用asList()方法时,数组就和列表链接在一起了.
当更新其中之一时,另一个将自动获得更新。
注意:仅仅针对对象数组类型,基本数据类型数组不具备该特性
(3) asList得到的数组是的没有add和remove方法的
为了对基本类型数组进行处理 可以使用下面的方法:
原理
该功能的实现主要依赖Array类
下面这个方法可以将任意数组变成集合。
public Collection arrayToCollection(Object[] array)
{
if(array.getClass().isArray()){
Collection col = new ArrayList();
int length = Array.getLength(array);
for(ing i=0; i<length; i++)
{
Object value = Array.get(array, i);
col.add(value);
}
return col;
}
return null;
}