BeanUtils copyProperties复制Arraylist
我知道BeanUtils可以将单个对象复制到其他对象。
是否可以复制一个arraylist。
例如:
FromBean fromBean = new FromBean("fromBean", "fromBeanAProp", "fromBeanBProp"); ToBean toBean = new ToBean("toBean", "toBeanBProp", "toBeanCProp"); BeanUtils.copyProperties(toBean, fromBean);
怎么做到这一点?
List fromBeanList = new ArrayList(); List toBeanList = new ArrayList(); BeanUtils.copyProperties(toBeanList , fromBeanList );
它不适合我。 谁能帮帮我吗。
提前致谢。
如果您有两个等于大小的列表,那么您可以执行以下操作
for (int i = 0; i < fromBeanList.size(); i++) { BeanUtils.copyProperties(toBeanList.get(i), fromBeanList.get(i)); }
如果您有一个列表来源,数据和列表目的地为空,解决方案是:
List
你可以尝试这样的事情
for(int i=0; i
希望这可以帮助..
哎呀它已经被某人解释了..
无论如何尝试它。
你可以做的是编写自己的通用副本类。
class CopyVector { private Class targetType; CopyVector(Class targetType) { this.targetType = targetType; } Vector copy(Vector src) { Vector target = new Vector (); for ( S s : src ) { T t = BeanUtils.instantiateClass(targetType); BeanUtils.copyProperties(s, t); target.add(t); } return target; } }
进一步的步骤也是使List类型通用 – 这假定您要复制Vector。
BeanUtils.copyProperties ,它只复制同名的属性。 所以,在ArrayList的情况下你不能这样做。
根据文件:
对于属性名称相同的所有情况,将属性值从原始bean复制到目标bean。