java如何用for循环遍历list集合?

来源:互联网 发布:邮箱直接注册淘宝号 编辑:程序博客网 时间:2024/06/06 13:21

和遍历数组不太一样,但是又有相似性,

用list.size() 表示list的长度(类似于数组的.length属性),用list.get(i)表示list中指定的元素(类似于数组的下标表示法:Arrays[i])。

具体代码如下:

if(list != null){
for(int i = 0 ; i < list.size() ; i++){
CountVoucherResponse countVoucher = list.get(i);
WeCahtQueryVoucherResponse weCahtQueryVoucherResponse = new WeCahtQueryVoucherResponse();
countVoucher.setCustomerId(customerId);
countVoucher.setTime(time);
List<CountVoucherResponse> queryInfoList = this.voucherDao.queryInfoByVoucherId(countVoucher);
if(queryInfoList != null && queryInfoList.size() > 0 ){
CountVoucherResponse queryInfo = queryInfoList.get(0);
weCahtQueryVoucherResponse.setNum(countVoucher.getNum());
weCahtQueryVoucherResponse.setMinTime(countVoucher.getMinTime());
weCahtQueryVoucherResponse.setMaxTime(countVoucher.getMaxTime());
StringBuffer voucherName = new StringBuffer();
if(queryInfo.getPromotionName() != null){
voucherName.append(queryInfo.getPromotionName());
weCahtQueryVoucherResponse.setPromotionName(voucherName.toString());
}
if(queryInfo.getTypeName() != null){
voucherName.append(queryInfo.getTypeName());
weCahtQueryVoucherResponse.setVoucherName(voucherName.toString());
}
if(queryInfo.getDiscountMoney() != null){
weCahtQueryVoucherResponse.setDiscountMoney(queryInfo.getDiscountMoney());
}
if(queryInfo.getDiscountNum() != null){
weCahtQueryVoucherResponse.setDiscountNum(queryInfo.getDiscountNum());
}
if(queryInfo.getTargetId() != null){
Goodsh goodsh = this.goodshDao.selectGoods(queryInfo.getTargetId());
if(goodsh != null){
weCahtQueryVoucherResponse.setTargetName(goodsh.getName());
}
}
weCahtQueryVoucherResponse.setCustomerId(customerId);
weCahtQueryVoucherResponse.setPromtionId(queryInfo.getPromtionId());
String[] voucherIds = new String[queryInfoList.size()];
for(int j = 0; j<queryInfoList.size(); j++){
voucherIds[j] = queryInfoList.get(i).getVoucherId();
}
weCahtQueryVoucherResponse.setVoucherIds(voucherIds);
queryResponseList.add(weCahtQueryVoucherResponse);
}
}
}

0 1
原创粉丝点击