1,Java深copy
/**
* 深copy一个list对象
* @param list
* @return
*/public static <T> List<T> deepCopy2(List<T> list, Class cc) throws Exception{
List<T> copy = new LinkedList();
if (list == null || list.size() < 0){
return copy;
}
for(T source : list){
T target = (T)cc.newInstance();
BeanUtils.copyProperties(source, target);
copy.add(target);
}
return copy;
}
2,通过反射属性深copy一个对象进入list
/**
* 通过反射属性给list新增对象
* @param list
* @param c
* @param <T>
* @throws Exception
*/private <T> void addByField(List<T> list, Class c) throws Exception{
T t = (T)c.newInstance();
Field field = c.getDeclaredField("index");
field.setAccessible(true);
field.set(t, 100);
list.add(t);
}
3,通过反射方法copy一个对象进入list
/**
* 通过反射方法给list新增对象
* @param list
* @param c
* @param <T>
* @throws Exception
*/private <T> void addByMothod(List<T> list, Class c) throws Exception{
T t = (T)c.newInstance();
Method setIndex = c.getDeclaredMethod("setIndex", Integer .class);
setIndex.invoke(t, 1);
list.add(t);
}