實際開發中,獲得到的list為重複的最後一條資料問題解決
阿新 • • 發佈:2018-12-03
在開發中迴圈把物件中的值賦給list,最後取得list進行返回。老是會出現獲得的list是重複的值,並且值為最後一個物件。
這就是開發中沒有宣告好物件和list的位置。都應該在外面宣告list和物件。
注意:物件要在迴圈裡重新初始化一次,否則最後一個數據會把前面的資料全部覆蓋掉。注意第9行
例如:
QueryInformationResponseVo returnVo = null; int count = 0; //收集頁面資訊 List<PrpCmain> blPrpCmain=new ArrayList<PrpCmain>(); List<QueryInformationResponseVo> returnList=new ArrayList<QueryInformationResponseVo>(); TreeSet seqKey = new TreeSet(sequence.keySet()); Iterator it = seqKey.iterator(); while(it.hasNext()){ returnVo = new QueryInformationResponseVo();//迴圈一次重新宣告 String flag = (String)it.next(); prpCmainSchema = (PrpCmain)sequence.get(flag); PrpCitemcar dbPrpCitemCarItem = new PrpCitemcar(); dbPrpCitemCarItem=(PrpCitemcar)cItemCarTable.get(prpCmainSchema.getPolicyNo()); returnVo.setFlag(flag); returnVo.setPolicyNo(prpCmainSchema.getPolicyNo()); returnVo.setCarOwner(dbPrpCitemCarItem.getCarOwner()); returnVo.setLicenseNo(dbPrpCitemCarItem.getLicenseNo()); returnVo.setEngineNo(dbPrpCitemCarItem.getEngineNo()); returnVo.setFrameNo(dbPrpCitemCarItem.getFrameNo()); returnVo.setInsuredName(prpCmainSchema.getInsuredName()); returnVo.setStartDate(StringUtils.dateTimeToStr(prpCmainSchema.getStartDate())); returnVo.setEndDate(StringUtils.dateTimeToStr(prpCmainSchema.getEndDate())); returnVo.setPolicyNo(prpCmainSchema.getPolicyNo()); String comName = prpDcompanyApi.translateCodeByPK(prpCmainSchema.getComCode()); returnVo.setComName(comName); returnList.add(returnVo); count++; }