嵌套 for 循环遍历同一集合,从所述集合中删除元素
Nested for loop over the same set, remove element from said set
我正在编写一个程序,在从一组集合中删除一个集合时遇到了问题。
问题
首先我将概述所需的功能。
给定一组存储整数值的集合,如下所示:
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
以及以下形式的约束列表:
((1, 2), (9, 10), (2, 9))
对于每个约束,我将遍历集合并根据约束合并和删除集合。
例如(1, 2) 表示 1 和 2 应该在同一个集合中,因此合并集合。
迭代约束后,剩余的集合应该是:
[[1,2,9,10], [3], [4], [5], [6], [7], [8]]
当集合中只有一个元素时(至少这是我确定的模式),我实现的方法可以合并和删除集合。
约束按顺序迭代,在应用最终约束之前,集合的状态如下:
[[1,2], [3], [4], [5], [6], [7], [8], [9,10]]
应用最终约束 (2,9) 后,这意味着应合并包含 2 和 9 的集合,代码产生以下输出:
[[1,2,9,10], [3], [4], [5], [6], [7], [8], [9,10]]
实施
下面的示例程序对此进行了演示:
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SetTest {
public static void main(String[] args) {
Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
Set<Integer> tempSet = new HashSet<Integer>();
//Populate original set with sets of integers 1-10
for (int i = 1; i <= 10; i++) {
tempSet.add(i);
originalSet.add(new HashSet<Integer>(tempSet));
tempSet.clear();
}
System.out.println("Original Sets: ");
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
//Generate constraints
List<Pair> pairSet = new LinkedList<Pair>();
pairSet.add(new Pair(1, 2));
pairSet.add(new Pair(9, 10));
pairSet.add(new Pair(2, 9));
System.out.println("Set of Pairs: ");
for (Pair p : pairSet) {
System.out.println(p.toString());
}
System.out.println("Merge and Remove Process");
//For each pair
for (Pair p : pairSet) {
System.out.println("Iteration::Merge sets containing " + p.toString());
boolean merged = false;
//Iterate over each set in the original set
for (Set s : originalSet) {
//If the set contains the first element of the pair and no merge has been performed
if (s.contains(p.getFirst()) && (merged == false)) {
//Find the set containing the second element of the pair
for (Set t : originalSet) {
if (t.contains(p.getSecond())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
//s.add(constraint.getB());
merged = true;
break;
}
//If the set contains the second element of the pair and no merge has been performed
if (s.contains(p.getSecond()) && (merged == false)) {
//Find the set containing the first element of the pair
for (Set t : originalSet) {
if (t.contains(p.getFirst())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
//s.add(constraint.getB());
merged = true;
break;
}
}
//Output the set after update
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
}
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + getFirst() + ", " + getSecond() + ")";
}
}
}
在 for 循环中删除集合的函数似乎不起作用,我不确定为什么。
for (Pair p : pairSet) {
for (Set s : originalSet) {
for (Set t : originalSet) {
originalSet.remove(t);
}
}
}
虽然合并似乎工作正常。
程序的输出如下:
Original Sets:
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Set of Pairs:
(1, 2)
(9, 10)
(2, 9)
Merge and Remove Process
Iteration::Merge sets containing (1, 2)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Iteration::Merge sets containing (9, 10)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]
Iteration::Merge sets containing (2, 9)
[1, 2, 9, 10]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]
最后一组 [9, 10] 不应该存在。
感谢您帮助我理解为什么删除函数没有达到预期的输出!
for (Set t : originalSet) {
if (t.contains(p.getFirst())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
你不能用那种方式删除它,如果你想从你迭代的集合中删除项目,你应该使用迭代器。请参阅 http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
因为你有两个嵌套循环遍历 originalSet
,修改集合会从两个迭代器下面拉出地毯。
只需一个循环,您就可以通过使用显式迭代器并调用 iterator.remove()
删除当前元素来修复它。
由于这在这里不起作用,您必须在修改原始 Set 的同时迭代 Set 的副本,或者在构建新 Set 时迭代原始 Set。
我会像这样使用迭代器:
package setproblem;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SetProblem {
public static void main(String[] args) {
Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
Set<Integer> tempSet = new HashSet<Integer>();
//Populate original set with sets of integers 1-10
for (int i = 1; i <= 10; i++) {
tempSet.add(i);
originalSet.add(new HashSet<Integer>(tempSet));
tempSet.clear();
}
System.out.println("Original Sets: ");
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
//Generate constraints
List<Pair> pairSet = new LinkedList<Pair>();
pairSet.add(new Pair(1, 2));
pairSet.add(new Pair(9, 10));
pairSet.add(new Pair(2, 9));
System.out.println("Set of Pairs: ");
for (Pair p : pairSet) {
System.out.println(p.toString());
}
System.out.println("Merge and Remove Process");
//For each pair
for (Pair p : pairSet) {
System.out.println("Iteration::" + p.toString());
Set<Integer> firstElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getFirst(), originalSet);
System.out.println("FES::" + firstElementSet.toString());
Set<Integer> secondElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getSecond(), originalSet);
System.out.println("SES::" + secondElementSet.toString());
removeElements(firstElementSet, originalSet);
removeElements(secondElementSet, originalSet);
originalSet.removeAll(firstElementSet);
originalSet.removeAll(secondElementSet);
firstElementSet.addAll(secondElementSet);
originalSet.add(firstElementSet);
for(Set<Integer> s : originalSet){
System.out.println(s.toString());
}
}
}
private static Set<Integer> getAllElementsFromOriginalSetContaining(Integer element, Set<Set<Integer>> originalSet) {
Set<Integer> tempSet = new HashSet<Integer>();
for(Set<Integer> s : originalSet){
if(s.contains(element)){
tempSet.addAll(s);
}
}
return new HashSet<Integer>(tempSet);
}
private static void removeElements(Set<Integer> elementSet, Set<Set<Integer>> originalSet) {
Iterator originalSetIterator = originalSet.iterator();
while(originalSetIterator.hasNext()) {
Set<Integer> s = (HashSet<Integer>)originalSetIterator.next();
if(s.equals(elementSet)){
System.out.println("Found set! " + elementSet.toString());
System.out.println("Found in originalSet! " + s.toString());
originalSetIterator.remove();
}
}
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + getFirst() + ", " + getSecond() + ")";
}
}
}
我正在编写一个程序,在从一组集合中删除一个集合时遇到了问题。
问题
首先我将概述所需的功能。
给定一组存储整数值的集合,如下所示:
[[1], [2], [3], [4], [5], [6], [7], [8], [9], [10]]
以及以下形式的约束列表:
((1, 2), (9, 10), (2, 9))
对于每个约束,我将遍历集合并根据约束合并和删除集合。 例如(1, 2) 表示 1 和 2 应该在同一个集合中,因此合并集合。
迭代约束后,剩余的集合应该是:
[[1,2,9,10], [3], [4], [5], [6], [7], [8]]
当集合中只有一个元素时(至少这是我确定的模式),我实现的方法可以合并和删除集合。 约束按顺序迭代,在应用最终约束之前,集合的状态如下:
[[1,2], [3], [4], [5], [6], [7], [8], [9,10]]
应用最终约束 (2,9) 后,这意味着应合并包含 2 和 9 的集合,代码产生以下输出:
[[1,2,9,10], [3], [4], [5], [6], [7], [8], [9,10]]
实施
下面的示例程序对此进行了演示:
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SetTest {
public static void main(String[] args) {
Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
Set<Integer> tempSet = new HashSet<Integer>();
//Populate original set with sets of integers 1-10
for (int i = 1; i <= 10; i++) {
tempSet.add(i);
originalSet.add(new HashSet<Integer>(tempSet));
tempSet.clear();
}
System.out.println("Original Sets: ");
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
//Generate constraints
List<Pair> pairSet = new LinkedList<Pair>();
pairSet.add(new Pair(1, 2));
pairSet.add(new Pair(9, 10));
pairSet.add(new Pair(2, 9));
System.out.println("Set of Pairs: ");
for (Pair p : pairSet) {
System.out.println(p.toString());
}
System.out.println("Merge and Remove Process");
//For each pair
for (Pair p : pairSet) {
System.out.println("Iteration::Merge sets containing " + p.toString());
boolean merged = false;
//Iterate over each set in the original set
for (Set s : originalSet) {
//If the set contains the first element of the pair and no merge has been performed
if (s.contains(p.getFirst()) && (merged == false)) {
//Find the set containing the second element of the pair
for (Set t : originalSet) {
if (t.contains(p.getSecond())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
//s.add(constraint.getB());
merged = true;
break;
}
//If the set contains the second element of the pair and no merge has been performed
if (s.contains(p.getSecond()) && (merged == false)) {
//Find the set containing the first element of the pair
for (Set t : originalSet) {
if (t.contains(p.getFirst())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
//s.add(constraint.getB());
merged = true;
break;
}
}
//Output the set after update
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
}
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + getFirst() + ", " + getSecond() + ")";
}
}
}
在 for 循环中删除集合的函数似乎不起作用,我不确定为什么。
for (Pair p : pairSet) {
for (Set s : originalSet) {
for (Set t : originalSet) {
originalSet.remove(t);
}
}
}
虽然合并似乎工作正常。
程序的输出如下:
Original Sets:
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Set of Pairs:
(1, 2)
(9, 10)
(2, 9)
Merge and Remove Process
Iteration::Merge sets containing (1, 2)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Iteration::Merge sets containing (9, 10)
[1, 2]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]
Iteration::Merge sets containing (2, 9)
[1, 2, 9, 10]
[3]
[4]
[5]
[6]
[7]
[8]
[9, 10]
最后一组 [9, 10] 不应该存在。 感谢您帮助我理解为什么删除函数没有达到预期的输出!
for (Set t : originalSet) {
if (t.contains(p.getFirst())) {
//Merge t with s
s.addAll(t);
originalSet.remove(t);
break;
}
}
你不能用那种方式删除它,如果你想从你迭代的集合中删除项目,你应该使用迭代器。请参阅 http://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
因为你有两个嵌套循环遍历 originalSet
,修改集合会从两个迭代器下面拉出地毯。
只需一个循环,您就可以通过使用显式迭代器并调用 iterator.remove()
删除当前元素来修复它。
由于这在这里不起作用,您必须在修改原始 Set 的同时迭代 Set 的副本,或者在构建新 Set 时迭代原始 Set。
我会像这样使用迭代器:
package setproblem;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SetProblem {
public static void main(String[] args) {
Set<Set<Integer>> originalSet = new HashSet<Set<Integer>>();
Set<Integer> tempSet = new HashSet<Integer>();
//Populate original set with sets of integers 1-10
for (int i = 1; i <= 10; i++) {
tempSet.add(i);
originalSet.add(new HashSet<Integer>(tempSet));
tempSet.clear();
}
System.out.println("Original Sets: ");
for (Set<Integer> set : originalSet) {
System.out.println(set.toString());
}
//Generate constraints
List<Pair> pairSet = new LinkedList<Pair>();
pairSet.add(new Pair(1, 2));
pairSet.add(new Pair(9, 10));
pairSet.add(new Pair(2, 9));
System.out.println("Set of Pairs: ");
for (Pair p : pairSet) {
System.out.println(p.toString());
}
System.out.println("Merge and Remove Process");
//For each pair
for (Pair p : pairSet) {
System.out.println("Iteration::" + p.toString());
Set<Integer> firstElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getFirst(), originalSet);
System.out.println("FES::" + firstElementSet.toString());
Set<Integer> secondElementSet = getAllElementsFromOriginalSetContaining((Integer)p.getSecond(), originalSet);
System.out.println("SES::" + secondElementSet.toString());
removeElements(firstElementSet, originalSet);
removeElements(secondElementSet, originalSet);
originalSet.removeAll(firstElementSet);
originalSet.removeAll(secondElementSet);
firstElementSet.addAll(secondElementSet);
originalSet.add(firstElementSet);
for(Set<Integer> s : originalSet){
System.out.println(s.toString());
}
}
}
private static Set<Integer> getAllElementsFromOriginalSetContaining(Integer element, Set<Set<Integer>> originalSet) {
Set<Integer> tempSet = new HashSet<Integer>();
for(Set<Integer> s : originalSet){
if(s.contains(element)){
tempSet.addAll(s);
}
}
return new HashSet<Integer>(tempSet);
}
private static void removeElements(Set<Integer> elementSet, Set<Set<Integer>> originalSet) {
Iterator originalSetIterator = originalSet.iterator();
while(originalSetIterator.hasNext()) {
Set<Integer> s = (HashSet<Integer>)originalSetIterator.next();
if(s.equals(elementSet)){
System.out.println("Found set! " + elementSet.toString());
System.out.println("Found in originalSet! " + s.toString());
originalSetIterator.remove();
}
}
}
public static class Pair<F, S> {
private F first; //first member of pair
private S second; //second member of pair
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
public void setFirst(F first) {
this.first = first;
}
public void setSecond(S second) {
this.second = second;
}
public F getFirst() {
return first;
}
public S getSecond() {
return second;
}
@Override
public String toString() {
return "(" + getFirst() + ", " + getSecond() + ")";
}
}
}