(JAVA)有集合A={1,2,3,4}和B={1,3,7,9,11},编写一个应用程序输出A和B
发布网友
发布时间:2022-05-06 07:10
我来回答
共1个回答
热心网友
时间:2023-10-08 15:37
?题目
编写程序求三个集合的交集
有三个集合a:{1,2,3,4,5}b{3,4,6,7,8}C:{4,5,8,9,0}.求出两两的交集,及三个集合的交集.采用C语言或Java语言编写.
猴壤捌85 2014-11-18
百度作业帮-是干什么的呢?让我来告诉你
优质解答
public class Test {
public static void main(String args[])
{
Set a = new HashSet(Arrays.asList(1,2,3,4,5));
Set b = new HashSet(Arrays.asList(3,4,6,7,8));
Set c = new HashSet(Arrays.asList(4,5,8,9,0));
Set aIntersectsB = intersects(a,b);
Set aIntersectsC = intersects(a,c);
Set bIntersectsC = intersects(b,c);
Set abc = intersects(a,bIntersectsC);
System.out.println("a ∩ b:" + aIntersectsB);
System.out.println("a ∩ c:" + aIntersectsC);
System.out.println("b ∩ c:" + bIntersectsC);
System.out.println("a ∩ b ∩ c:" + abc);
}
private static Set intersects(Set set1,Set set2) {
Set intersectSet = new HashSet(set1);
intersectSet.retainAll(set2);
return intersectSet;
}
}