简体   繁体   English

在迭代器中添加 arraylist 对象而循环会导致第二个对象覆盖一个对象

[英]adding arraylist object in iterator while loop causes second object to over write one object

I have an arraylist which is iterated using an iterator.我有一个使用迭代器迭代的数组列表。 the iterated data is passed to a method getDetails as a parameter.迭代数据作为参数传递给方法 getDetails。 now the method returned data is written to an arraylist detailsList.现在方法返回的数据被写入一个arraylist detailsList。 So if the arraylist DTOList has two different objects, the second object is over writing the first one.因此,如果 arraylist DTOList 有两个不同的对象,则第二个对象会覆盖第一个对象。 So when i read the list, it has two same object contents.所以当我阅读列表时,它有两个相同的对象内容。

List < BRType > detailsList= new ArrayList < BRType > ();
        List < RDTO > rDTOList = crList;
        List < ADTO > aDTOList = daList;
        Iterator < RDTO > rDTOIterator = rDTOList.iterator();
         Iterator < ADTO > aDTOIterator = aDTOList.iterator();
        while (rDTOIterator.hasNext() && aDTOIterator.hasNext()) {
            RDTO rDTO = rDTOIterator.next();
            ADTO aDTO = aDTOIterator.next();

        detailsList.add(getDetails(rDTO , aDTO , bookingRuleType,null));
    }

here during the first while loop the detailsListhas the code of 1, and during second run it has 2 .在第一个 while 循环中, detailsList 的代码为 1 ,第二次运行时它的代码为 2 。 so when i run the below code i get 2 and 2所以当我运行下面的代码时,我得到 2 和 2

  for(BRType d : detailsList){
                System.out.println(d.getCode());

            }

I got it!!!我知道了!!! it was a small mistake.这是一个小错误。 in this method call在这个方法调用中

getMarCancelPolicyDepositRuleBySWCancelPolicyDepositRule(cancelDepositRuleDTO, cancelDepositAmountDTO, bookingRuleType,null)

the bookingRuleType parameter to the method, that object reference was created outside the while loop.该方法的bookingRuleType参数,该对象引用是在 while 循环之外创建的。 so i put this BookingRuleType bookingRuleType = new BookingRuleType();所以我把这个BookingRuleType bookingRuleType = new BookingRuleType(); inside the while loop and it worked like charm在 while 循环中,它的作用就像魅力一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM