java.lang.reflect.Array 对任意数组进行操作
private Object items;
private Collection collection;
public void setItems(Object items){
this.items = items;
if(items instanceof Collection){
collection = (Collection) items;
}
if(items instanceof Map){
Map map = (Map) items;
collection = map.entrySet();
}
if(items.getClass.isArray()){
this.collection = new ArrayList();
int length = Array.getLength(items);
for(int i = 0;i < length; i++){
Object value = Array.get(items , i);
this.collection.add(value);
}
}
}
本文介绍了一种在Java中将不同类型的数组和集合相互转换的方法。通过实例展示了如何使用反射来处理任意类型的数组,并将其元素收集到集合中,同时支持Collection和Map类型的处理。
5584

被折叠的 条评论
为什么被折叠?



