java 遍历中为对象添加元素
Posted qqzhulu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 遍历中为对象添加元素相关的知识,希望对你有一定的参考价值。
publie Object getaddress(){
List<MemberAddress> memberAddressList = new ArrayList<MemberAddress>();
try {
// 通过memberId查询用户所有地址
memberAddressList = memberAddressService.getByMemberId(member.getId());
} catch (Exception e) {
return new APIResult(GlobalConstantsUtil.GENERAL_ERROR, "获取字段失败");
}
// 若memberAddressList集合为空,返回为空
if (memberAddressList == null || memberAddressList.size() == 0) {
return APIResult.createSuccess();
}
// 创建MemberAddressVO3集合
List<MemberAddressVO3> list = new ArrayList<MemberAddressVO3>();
// 遍历MemberAddress,将其值赋予MemberAddressVO3对象
for (int i = 0; i < memberAddressList.size(); i++) {
MemberAddress memberAddress = memberAddressList.get(i);
MemberAddressVO3 memberAddressVO3 = new MemberAddressVO3(); //新创建对象(在循环中)
memberAddressVO3.setMemberAddressId(memberAddress.getId());
memberAddressVO3.setAddressStatus(memberAddress.getAddressStatus());
memberAddressVO3.setCityId(memberAddress.getCityId());
memberAddressVO3.setConsignee(memberAddress.getConsignee());
memberAddressVO3.setContactMobilePhone(memberAddress.getContactMobilePhone());
memberAddressVO3.setCreateTime(memberAddress.getCreateTime());
memberAddressVO3.setDetail(memberAddress.getDetail());
memberAddressVO3.setMemberId(memberAddress.getMemberId());
memberAddressVO3.setAddressStatusDefault(false);
if ((memberAddress.getAddressStatus()
& GlobalConstantsUtil.MEMBER_ADDRESS_DEFAULT) == GlobalConstantsUtil.MEMBER_ADDRESS_DEFAULT) {
memberAddressVO3.setAddressStatusDefault(true);
}
// 将获得的MemberAddressVO3对象添加至MemberAddressVO3的list集合中
list.add(memberAddressVO3);
}
retrun list;
}
以上是关于java 遍历中为对象添加元素的主要内容,如果未能解决你的问题,请参考以下文章
java多线程同时向一个数组arraylist添加元素,遍历这个集合
创建一个arraylist集合 添加10个元素 然后用foreach遍历并输出
jQuery$.each循环遍历详解,各种取值对比,$.each遍历数组对象Dom元素二维数组双层循坏类json数据等等