资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java差集代码 等差数列java代码

Java中的Set类差集问题?差集为什么是1,2不是1,2,8,9,;

如下英文是Guava中注释原文。意思是说该方法返回只存在于set1独有的数据,至于set2中独有数据和set1和set2交集的数据直接忽略。而且返回的只是不可变的Set视图,不会修改原set中数据。

创新互联一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!为您提供成都网站设计、做网站、成都网页设计、微信小程序定制开发、成都网站开发、成都网站制作、成都软件开发、重庆APP开发公司是成都本地专业的网站建设和网站设计公司,等你一起来见证!

/**

* Returns an unmodifiable view of the difference of two sets. The

* returned set contains all elements that are contained by set1 and

* not contained by set2. set2 may also contain elements not

* present in set1; these are simply ignored. The iteration order of

* the returned set matches that of set1.

*

* Results are undefined if set1 and set2 are sets based

* on different equivalence relations (as HashSet, TreeSet,

* and the keySet of an IdentityHashMap all are).

*/

用java编写程序,求集合的并集、交集和差集

public static void  main(String[] args) {

Integer[] A = {1,2,3,4};

Integer[] B = {1,3,7,9,11};

ListInteger listA = Arrays.asList(A);

ListInteger listB = Arrays.asList(B);

ListInteger jiaoji = new ArrayListInteger();

for(Integer a:listA){

if(listB.contains(a)){

jiaoji.add(a);

}

}

System.out.println(jiaoji);

ListInteger bingji = new ArrayListInteger();

for(Integer a:listA){

if(!bingji.contains(a)){

bingji.add(a);

}

}

for(Integer b:listB){

if(!bingji.contains(b)){

bingji.add(b);

}

}

System.out.println(bingji);

ListInteger chaji = new ArrayListInteger();

for(Integer a:listA){

if(!listB.contains(a)){

chaji.add(a);

}

}

System.out.println(chaji);

}

java 求交集 并集 差集

import java.util.*;

public class ArrayTest {

public static void main(String[] args) {

int[] a = {1,6,4,5,2,3,};

int[] b = {2,3,4,56,7,8,99};

int[] t = ArrayTest.并集(a, b);

for(int i:t)System.out.print(i+" ");

System.out.println();

t = ArrayTest.交集(a, b);

for(int i:t)System.out.print(i+" ");

}

static int[] 并集(int[] a,int[] b){

Arrays.sort(a);

Arrays.sort(b);

int[] t = new int[a.length];

System.arraycopy(a,0,t,0,t.length);

out:

for(int i:b){

for(int j:a){

if(i==j)continue out;

}

t=putInt(t,i);

}

Arrays.sort(t);

return t;

}

static int[] 交集(int[] a,int[] b){

Arrays.sort(a);

Arrays.sort(b);

int[] t = new int[0];

for(int i:a){

for(int j:b){

if(i==j){

t=putInt(t,i);

break;

}

}

}

return t;

}

static int[] putInt(int[] a,int i){

int[] t = new int[a.length+1];

System.arraycopy(a, 0,t,0,a.length);

t[a.length]=i;

return t;

}

}

//做了交集,并集,差集自己想吧


网站标题:java差集代码 等差数列java代码
当前URL:http://cdkjz.cn/article/hpoesc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220