问题描述
StringA={A,B,C,D,E}StringB={C,D,F,G,I}求结果1.{A,B,E}2.{C,D}3.{F,G,I}
解决方案
解决方案二:
要用java实现
解决方案三:
importjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F,G,I";//得到两个集合对应的数组String[]tempA=A.split(",");String[]tempB=B.split(",");List<String>list1=newArrayList<String>();//保存A与B差集List<String>list2=newArrayList<String>();//保存交集List<String>list3=newArrayList<String>();//保存B与A的差集for(inti=0;i<tempA.length;i++){if(!find(tempB,tempA[i]))list1.add(tempA[i]);elselist2.add(tempA[i]);}for(inti=0;i<tempB.length;i++){if(!find(tempA,tempB[i]))list3.add(tempB[i]);}System.out.println("A与B的差集:"+list1);System.out.println("A与B的交集:"+list2);System.out.println("B与A的差集:"+list3);}/***判断数组arr中是否包含字符串str,包含则返回true,否则返回false*@paramarr*@paramstr*@return*/privatestaticbooleanfind(String[]arr,Stringstr){booleanflag=false;for(inti=0;i<arr.length;i++){if(arr[i].equals(str)){flag=true;break;}}returnflag;}}
解决方案四:
引用2楼hudie1234567的回复:
Javacodeimportjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F,G,I";……
真快
解决方案五:
引用3楼yewuqing007的回复:
引用2楼hudie1234567的回复:Javacodeimportjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,……
呵呵,一般般啦!
解决方案六:
LZ。。。。。你这是求交集吧,不是并集
解决方案七:
根据楼主的要求做的,标题上楼主可能写错了!
解决方案八:
String[]A=newString[]{"A","B","C","D","E"};String[]B=newString[]{"C","D","F","G","I"};List<String>aList=newArrayList<String>(Arrays.asList(A));List<String>bList=newArrayList<String>(Arrays.asList(B));List<String>cList=newArrayList<String>(aList);cList.retainAll(bList);aList.removeAll(cList);bList.removeAll(cList);System.out.println(aList);System.out.println(bList);System.out.println(cList);
提供一个短版的
解决方案九:
引用2楼hudie1234567的回复:
Javacodeimportjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F,G,I";……
借用一下你的代码,可以通过结合的交并差方法Collection.addAll并Collection.removeAll差Collection.retainAll交Arrays.asList得到一个集合importjava.util.*;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F,G,I";//得到两个集合对应的数组String[]tempA=A.split(",");String[]tempB=B.split(",");List<String>list1=Arrays.asList(tmpeA).removeAll(Arrays.asList(tmppB));//差List<String>list2=Arrays.asList(tmpeA).retainAll(Arrays.asList(tmppB));//交List<String>list3=Arrays.asList(tmppB).retainAll(list2);//差System.out.println("A与B的差集:"+list1);System.out.println("A与B的交集:"+list2);System.out.println("B与A的差集:"+list3);}}
解决方案十:
++++引用8楼qybao的回复:
引用2楼hudie1234567的回复:Javacodeimportjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F……
解决方案十一:
该回复于2010-12-15 13:52:27被版主删除
解决方案十二:
既然是集合,当然用集合来解呀。集合是无序的,结果与楼主期待的相同,但是顺序不同。如果想要顺序可以用java的可排序集合//测试用代码publicvoidtestSetHandler(){/*StringA={A,B,C,D,E}StringB={C,D,F,G,I}求结果1.{A,B,E}2.{C,D}3.{F,G,I}*/String[]arrA=newString[]{"A","B","C","D","E"};String[]arrB=newString[]{"C","D","F","G","I"};Set<String>setA=newHashSet<String>();Set<String>setB=newHashSet<String>();setA.addAll(Arrays.asList(arrA));setB.addAll(Arrays.asList(arrB));//结果1A-BSet<String>setA_B=geDifferenceSet(setA,setB);outPrintResult("结果1A-B",setA_B);//结果2AnBSet<String>setBnA=getSameSet(setA,setB);outPrintResult("结果2AnB",setBnA);//结果3B-ASet<String>setB_A=geDifferenceSet(setB,setA);outPrintResult("结果3B-A",setB_A);}privatevoidoutPrintResult(Stringdescription,Set<String>set){System.out.println(description+":");Iterator<String>ite=set.iterator();while(ite.hasNext()){StringcurrentStr=ite.next();System.out.println(""+currentStr);}}//合集求出publicSet<String>getUnionSet(Set<String>...sets){Set<String>unionSet=newHashSet<String>();for(Set<String>set:sets){unionSet.addAll(set);}returnunionSet;}//交集求出=合集-差集publicSet<String>getSameSet(Set<String>...sets){Set<String>sameSet=newHashSet<String>();Set<String>unionSet=getUnionSet(sets);sameSet=unionSet;for(Set<String>set:sets){sameSet=geDifferenceSet(sameSet,geDifferenceSet(unionSet,set));}returnsameSet;}//差集求出publicSet<String>geDifferenceSet(Set<String>setSrc,Set<String>...sets){Set<String>unionSet=newHashSet<String>();unionSet.addAll(setSrc);for(Set<String>set:sets){unionSet.removeAll(set);}returnunionSet;}
解决方案十三:
引用8楼qybao的回复:
引用2楼hudie1234567的回复:Javacodeimportjava.util.ArrayList;importjava.util.List;publicclassJihe{publicstaticvoidmain(String[]args){StringA="A,B,C,D,E";StringB="C,D,F……
retainAll受教了。谢谢~
解决方案十四:
上面说的不错,你可以看看........