常州制作网站公司,广州建筑公司实力排名,湛江网站建设模板定位工厂,秦皇岛建设网官网前两天给我出了一道题#xff0c;求数组的并集和交集#xff0c;然后我试着写一下#xff0c;很尴尬#xff0c;由于长时间没有写过代码#xff0c;一开始数组是如何定义的给忘了。当时我说了我的思路#xff0c;不过也是很low的做法#xff0c;查阅网上的一些资料…前两天给我出了一道题求数组的并集和交集然后我试着写一下很尴尬由于长时间没有写过代码一开始数组是如何定义的给忘了。当时我说了我的思路不过也是很low的做法查阅网上的一些资料实现的很厉害不过对于我这种习惯了看112这种操作的人难免有些为难。好了牢骚发够了这里提供一种思路在我看来很简单。public class SumandRetain {Set mnew HashSet();ArrayList a1new ArrayList();ArrayList b1new ArrayList();//求两个数组的并集(利用Set的去重机制)public Set sum(int a[],int b[]) {for(int i0;im.add(a[i]);}for(int j0;jm.add(b[j]);}return m;}//求两个数组的交集(利用集合类的retainAll()方法)public ArrayList retain(int a[],int b[]){for(int i0;ia1.add(a[i]);}for(int j0;jb1.add(b[j]);}b1.retainAll(a1);return b1;}}再编写主方法public static void main(String[] args) {// TODO Auto-generated method stubint a[] {1,3,5,7};int b[] {2,3,4,9};SumandRetain sumandReatainnew SumandRetain();System.out.println(sumandReatain.sum( a,b));System.out.println(sumandReatain.retain(a, b));}