如何在java中的if-else循环和removeAll中使用迭代器?
我有以下问题:我确实在“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将所有要删除的元素放在单独的列表或集合中,并在循环结束后将其删除。在删除全部的情况下,设置一个布尔值,并在 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 aremove()
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.使用带索引的普通 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