「java两个数组取差集」获取两个数组的差集

博主:adminadmin 2023-01-19 10:39:07 334

今天给各位分享java两个数组取差集的知识,其中也会对获取两个数组的差集进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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集合求差值和并集!

差集

ArrayListString stuList = new ArrayListString();

stuList.add("aa");

stuList.add("bb");

stuList.add("cc");

stuList.add("dd");

ArrayListString stuList2 = new ArrayListString();

stuList2.add("bb");

stuList2.add("cc");

stuList2.add("ee");

stuList2.add("ff");

for (String s : stuList2) {

if (stuList.contains(s)) {

stuList.remove(s);

} else {

stuList.add(s);

}

}

System.out.println(stuList2);

合集

ArrayList stuList = new ArrayList();

stuList.add("aa");

stuList.add("bb");

stuList.add("cc");

stuList.add("dd");

ArrayList stuList2 = new ArrayList();

stuList2.add("bb");

stuList2.add("cc");

stuList2.add("ee");

stuList2.add("ff");

Set set=new HashSet();

for (Object object : stuList) {

set.add(object);

}

for (Object object : stuList2) {

set.add(object);

}

System.out.println(set);

java找到两个list的交集并集差集

//交集

set1.retainAll(set2);

//差集

set1.removeAll(set2);

//并集1

set1.addAll(set2);

关于java两个数组取差集和获取两个数组的差集的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。