对于两个数组
arr1=[1,3,4,5,8,9]
arr2=[2,3,7,8,9]
求出交集与并集
test.java
1 import java.util.ArrayList; 2 import java.util.Collections; 3 4 5 public class test1 { 6 public static void main(String[] args) { 7 ArrayListtmplist=new ArrayList (); 8 int[] arr1={1,3,4,5,8,9}; 9 int[] arr2={2,3,7,8,9};10 tmplist=Intersection(arr1,arr2);11 OutPut(tmplist);12 tmplist.clear();13 tmplist=Union(arr1,arr2);14 OutPut(tmplist);15 }16 private static ArrayList Intersection(int[] a1,int[] a2){17 ArrayList list=new ArrayList ();18 for(int i=0;i Union(int[] a1,int[] a2){26 ArrayList list1=new ArrayList ();27 ArrayList list2=new ArrayList ();28 for(int i:a1)29 list1.add(i);30 for(int i:a2)31 list2.add(i);32 list1.removeAll(list2);33 list2.addAll(list1);34 return list2;35 }36 37 private static void OutPut(ArrayList list){38 Collections.sort(list);39 if(list.size()==0)40 System.out.println("null");41 else42 for(int i:list)43 System.out.print(i);44 System.out.println("\n");45 }46 }