packageLabEight;
importjava.util.*;
publicclassSetDemo{
publicstaticvoidmain(String[]args)
{
int[]a={2,5,9,13};
int[]b={1,3,6,9,15,21};
HashSetsetA=newHashSet();
HashSetsetB=newHashSet();
for(inti=0;i<a.length;i++)
{
//将数组a中元素添加到setA对象中
}
for(inti=0;i<b.length;i++)
{
//将数组b中元素添加到setB对象中
}
System.out.println("合并前集合A的数据:");
Iteratorit=setA.iterator();
while(it.hasNext())
{
System.out.print(it.next()+"");
}
//将setB中元素合并到setA中
System.out.println("\n合并后集合B的数据:");
it=setA.iterator();
while(it.hasNext())
{
System.out.print(it.next()+"");
}
}
}
将集合A和B中的元素合并到集合A中
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 嘉祐-小萝卜算子 2022-03-30 10:24关注
public static void main(String[] args) { int[] a={2,5,9,13}; int[] b={1,3,6,9,15,21}; //这里必须指定HashSet的类型 HashSet<Integer> setA=new HashSet<Integer>(); HashSet<Integer> setB=new HashSet<Integer>(); for(int i=0;i<a.length;i++) { //将数组a中元素添加到setA对象中 setA.add(a[i]); } for(int i=0;i<b.length;i++) { //将数组b中元素添加到setB对象中 setB.add(b[i]); } System.out.println("合并前集合A的数据:"); Iterator it=setA.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); } //将setB中元素合并到setA中 //这里必须指定Iterator的类型 Iterator<Integer> itB=setB.iterator(); while(itB.hasNext()) { setA.add(itB.next()); } System.out.println("\n合并后集合A的数据:"); it=setA.iterator(); while(it.hasNext()) { System.out.print(it.next()+" "); } }
可以参考一下
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 运动想象脑电信号数据集.vhdr
- ¥15 三因素重复测量数据R语句编写,不存在交互作用
- ¥15 微信会员卡等级和折扣规则
- ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
- ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
- ¥15 gdf格式的脑电数据如何处理matlab
- ¥20 重新写的代码替换了之后运行hbuliderx就这样了
- ¥100 监控抖音用户作品更新可以微信公众号提醒
- ¥15 UE5 如何可以不渲染HDRIBackdrop背景
- ¥70 2048小游戏毕设项目