//集合A
HashSet hashSetA = new HashSet();
Scanner scannerA = new Scanner(System.in);
boolean bA = true;
while (bA) {
int iA = scannerA.nextInt();
if (iA != 0) {
hashSetA.add(iA);
}
if (iA == 0) {
bA = false;
}
}
//集合B
HashSet hashSetB = new HashSet();
Scanner scannerB = new Scanner(System.in);
boolean bB = true;
while (bB) {
int iB = scannerB.nextInt();
if (iB != 0) {
hashSetB.add(iB);
}
if (iB == 0) {
bB = false;
}
}
//集合运算
HashSet result = new HashSet();
//并集
result.addAll(hashSetA);
result.addAll(hashSetB);
System.out.println(result.toString().replace("[", “”).replace("]", “”).replace(",", “”));
//差集
result.addAll(hashSetA);
result.removeAll(hashSetB);
System.out.println(result.toString().replace("[", “”).replace("]", “”).replace(",", “”));
//交集
result.addAll(hashSetA);
result.retainAll(hashSetB);
System.out.println(result.toString().replace("[", “”).replace("]", “”).replace(",", “”));
大一下导出作业编码题集合运算
最新推荐文章于 2024-07-20 23:59:39 发布