任务描述:
现给出两个数组,每个数组都有10个元素,请求两个数组的交集,如交集为空,请输出 NULL,否则输出交集元素(数组元素取值范围0~1000)。
任务要求:
代码示例:
/**
* 计算两个数组的交集。
* 该程序首先通过用户输入创建两个整数数组,然后找出这两个数组中相同的元素,
* 并将这些相同的元素存储到一个新的数组中,最后打印出这个新数组(即两个数组的交集)。
*/
package April_2024;
import java.util.Scanner;
public class a240421_2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 输入第一个数组的元素
System.out.println("第一个数组元素:");
int[] arr1 = new int[10];
for (int i = 0; i < 10; i++) {
arr1[i] = sc.nextInt();
}
// 输入第二个数组的元素
System.out.println("第二个数组元素:");
int[] arr2 = new int[10];
for (int i = 0; i < 10; i++) {
arr2[i] = sc.nextInt();
}
// 寻找a