我是靠谱客的博主 粗犷大树,这篇文章主要介绍List使用Iterator遍历的坑,现在分享给大家,希望可以做个参考。

 

              写法一:
              Iterator<QcSampleDTO> it = qclist.iterator();
              //排除尾箱容器
               while (it.hasNext()) {
              if(it.next().getContainerId().equals(tailDTO.getContainerId())
                && it.next().getLotId().equals(tailDTO.getLotId())) {
                                it.remove();
                   }
               }
               
                出现了两次 it.next() ,如果list集合长度为2,就不会第二次进入遍历。


               写法二:
               QcSampleDTO tailDTO = tailList.get(0);
                Iterator<QcSampleDTO> it = qclist.iterator();
                //排除尾箱容器
                while (it.hasNext()) {
                QcSampleDTO sampleDTO = it.next();
                 if (sampleDTO.getContainerId().equals(tailDTO.getContainerId())
                                && sampleDTO.getLotId().equals(tailDTO.getLotId())) {
                      it.remove();
                   }
               }

                

写法一是个坑,it.next()使用一次,下标加一,写法二正确。

最后

以上就是粗犷大树最近收集整理的关于List使用Iterator遍历的坑的全部内容,更多相关List使用Iterator遍历内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部