如何在java中的if-else循环和removeAll中使用迭代器?

发布于 2024-12-02 03:38:47 字数 1251 浏览 2 评论 0原文

我有以下问题:我确实在“if”的第一部分中使用迭代器来删除 S 的元素,但我不知道如何在“if”中再次使用相同的迭代器从 S 中删除整个集合 S3 “其他”部分。有什么想法吗?先感谢您!

public void f(RewritingNode x, Set<RewritingNode>S0){
      Set<RewritingNode> S1 = new HashSet<RewritingNode>();
      Set<RewritingNode> S3 = new HashSet<RewritingNode>();
      S1.addAll(x.children);
      S0.addAll(S1);
      Set<RewritingNode> S = new HashSet<RewritingNode>();
      S.addAll(S1);

      while (!S.isEmpty()){
          for (Iterator<RewritingNode> iter_y= S.iterator(); iter_y.hasNext();) {
              RewritingNode y = iter_y.next();

              RewritingNode y = iter_y.next();

              if(S0.containsAll(y.parents)||y.parents.isEmpty()){
                 iter_y.remove();
              }
              else {
                  S3.add(y);                      
                  S.addAll(S1);             
                  S.removeAll(S3);
              } 
          }
    }

    Set<RewritingNode> removedChildren = new HashSet<RewritingNode>();
    removedChildren.addAll(S1);
    removedChildren.removeAll(S3);

    for(RewritingNode x1 :removedChildren){
        x1.parents.removeAll(x1.parents);
        f(x1,S0);
    }
}

I have the following problem: I do use iterator in the first part of "if" to remove an element of S, but I don't have a clue about how to remove the whole set S3 from S using the same iterator again in the "else" part. Any ideas? Thank you in advance!

public void f(RewritingNode x, Set<RewritingNode>S0){
      Set<RewritingNode> S1 = new HashSet<RewritingNode>();
      Set<RewritingNode> S3 = new HashSet<RewritingNode>();
      S1.addAll(x.children);
      S0.addAll(S1);
      Set<RewritingNode> S = new HashSet<RewritingNode>();
      S.addAll(S1);

      while (!S.isEmpty()){
          for (Iterator<RewritingNode> iter_y= S.iterator(); iter_y.hasNext();) {
              RewritingNode y = iter_y.next();

              RewritingNode y = iter_y.next();

              if(S0.containsAll(y.parents)||y.parents.isEmpty()){
                 iter_y.remove();
              }
              else {
                  S3.add(y);                      
                  S.addAll(S1);             
                  S.removeAll(S3);
              } 
          }
    }

    Set<RewritingNode> removedChildren = new HashSet<RewritingNode>();
    removedChildren.addAll(S1);
    removedChildren.removeAll(S3);

    for(RewritingNode x1 :removedChildren){
        x1.parents.removeAll(x1.parents);
        f(x1,S0);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林迷了鹿 2024-12-09 03:38:47

将所有要删除的元素放在单独的列表或集合中,并在循环结束后将其删除。在删除全部的情况下,设置一个布尔值,并在 while 循环结束后再次执行此操作。或者只需将所有元素添加到删除元素列表中,并在 while 循环结束后删除它们,否则您将得到某种并发修改异常。

更新

尝试使用队列而不是您在这里所做的任何事情。类似于 LinkedList 的队列 具有 FIFO 顺序。 LinkedList 有一个 remove() 方法返回第一个元素并将其删除。用它来获取第一个元素并进行比较,如果需要保留它,则将其再次添加到列表中,它将成为最后一个元素。继续执行此操作,直到列表为空为止,这应该可以为您完成。

这应该比您的代码简单得多,并且不需要迭代器或多个集合。如果出于某种原因,当 remove() 方法返回元素时,您确实需要将删除的元素添加到集合中(或者您想要保留的元素),请将其添加到您想要的任何集合中。

Put all the elements you want to remove in a separate list or set and remove them after your loop finishes. In the case of remove all, set a boolean and again, do this after the while-loop ends. Or just add all the element to your remove elements list and remove them after the while loop ends otherwise you'll get some sort of concurrent modification exception.

Update

Try to use a queue instead of whatever you are doing here. A queue like LinkedList which has a FIFO order. LinkedList has a remove() method which returns the first element and removes it. Use it to fetch the first element and compare it, if you need to keep it, add it to the list again and it'll become the last element. keep doing this till the list is empty and that should do it for you.

This should be far simpler than your code and no need for iterators or multiple sets. If for whatever reason you do need to add removed elements to a set (or event he ones you want to keep) when the remove() method returns the element, add it to whatever set you want.

不知所踪 2024-12-09 03:38:47

使用带索引的普通 For 循环而不是迭代器或增强的 For 循环,我认为在遍历迭代器时不能执行 removeAll

Use a normal For loop with index instead of iterator or enhanced For loop, I think you cannot do removeAll while going through an iterator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文