集合运算








解析: 根据交集、并集和余集的特性,封装了三个方法分别进行处理。因为交集、并集和余集中的数字个数都是不确定的,所以用集合ArrayList接收数据比较适合,再将集合转变成数组,对数组进行升序处理就得到了最终的结果。



package _4_9_test;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;

/* 集合运算
 * B在A中的余集,A中除去A和B相交元素的所有元素
 * */
public class NightyThree {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scanner = new Scanner(System.in);

		// 得到两个输入的数组
		int n = scanner.nextInt();
		int A[] = new int[n];
		for (int i = 0; i < n; i++) {
			A[i] = scanner.nextInt();
		}

		int m = scanner.nextInt();
		int B[] = new int[m];
		for (int i = 0; i < m; i++) {
			B[i] = scanner.nextInt();
		}

		// 调用三个方法,返回三个集合的结果
		Object jiaoji[] = jiao(A, B);
		Object bingji[] = bing(A, B);
		Object yuji[] = yu(A, B);

		// 输出三个集合的结果
		for (Object i : jiaoji) {
			System.out.print(i + " ");
		}
		System.out.println();

		for (Object i : bingji) {
			System.out.print(i + " ");
		}
		System.out.println();

		for (Object i : yuji) {
			System.out.print(i + " ");
		}

	}

	// 交集
	public static Object[] jiao(int a[], int b[]) {
		ArrayList<Integer> arrayList = new ArrayList<>();

//		如果两个数组中有相同的数,就添加到集合中
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < b.length; j++) {
				if (a[i] == b[j]) {
					arrayList.add(a[i]);
				}
			}
		}

//		将集合转成数组
		Object[] result = new Object[arrayList.size()];
		result = arrayList.toArray();
//		对数组进行升序排序
		Arrays.sort(result);

		return result;
	}

	// 并集
	public static Object[] bing(int a[], int b[]) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();

//		先将第一个数组中有,第二个数组中没有的数字添加进集合中。
//		再将第二个数组中所有的数字添加进集合中
		for (int i = 0; i < a.length; i++) {
			boolean flag = true;
			for (int j = 0; j < b.length; j++) {
				if (a[i] == b[j]) {
					flag = false;
					break;
				}
			}
			if (flag) {
				arrayList.add(a[i]);
			}
		}

		for (int i = 0; i < b.length; i++) {
			arrayList.add(b[i]);
		}

		Object[] result = new Object[arrayList.size()];
		result = arrayList.toArray();
		Arrays.sort(result);

		return result;
	}

	// 余集
	public static Object[] yu(int a[], int b[]) {
		ArrayList<Integer> arrayList = new ArrayList<Integer>();

//		将第一个数组中有,第二个数组中没有的数字添加进集合中。
		for (int i = 0; i < a.length; i++) {
			boolean flag = true;
			for (int j = 0; j < b.length; j++) {
				if (a[i] == b[j]) {
					flag = false;
				}
			}
			if (flag) {
				arrayList.add(a[i]);
			}
		}

		Object[] result = new Object[arrayList.size()];
		result = arrayList.toArray();
		Arrays.sort(result);

		return result;
	}

}

本文作者:西红柿里没有番茄

本文链接:https://www.cnblogs.com/lyd447113735/p/12733742.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   西红柿里没有番茄  阅读(197)  评论(0编辑  收藏  举报
编辑推荐:
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
阅读排行:
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· PPT革命!DeepSeek+Kimi=N小时工作5分钟完成?
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 赶AI大潮:在VSCode中使用DeepSeek及近百种模型的极简方法
· DeepSeek企业级部署实战指南:从服务器选型到Dify私有化落地
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起