如何在Java中查找两个数组的并集?


在Java中,数组是一个对象。它是一种非原始数据类型,用于存储相同数据类型的数值。

根据题意,我们需要找到两个数组的并集。并集指的是合并在一起,而数组的并集指的是一个新的数组,它包含来自输入数组的所有唯一元素。

让我们探讨这篇文章,看看如何使用Java编程语言来实现。

展示一些实例

实例1

Suppose the first array is {0, 3, 5, 6, 9, 1}.
And the second array is {1, 2, 6, 10, 8, 7}.

执行并集操作后的结果将是:

Union of two arrays is: [0, 1, 2, 3, 5, 6, 7, 8, 9, 10]

实例2

Suppose the first array is {0, 5, 11, 7, 9, 3}.
And the second array is {1, 2, 4, 5, 12, 7}.

执行并集操作后的结果将是:

Union of two arrays is: [0, 1, 2, 3, 4, 5, 7, 9, 11, 12]

实例3

Suppose the first array is {12, 13, 5, 16, 9, 19}.
And the second array is {16, 2, 60, 9, 8, 5}.

执行并集操作后的结果将是:

Union of two arrays is: [16, 2, 19, 5, 8, 9, 12, 60, 13]

算法

  • 步骤1 - 声明并初始化一个整数数组。

  • 步骤2 - 初始化HashSet来存储第一个和第二个数组的并集。

  • 步骤3 - 将第一个数组和第二个数组的所有元素添加到集合中。

  • 步骤4 - 打印结果。

语法

要获取数组的长度(数组中的元素个数),数组有一个内置属性,即length

以下是它的语法:

array.length

要将数组转换为元素列表,java.util包的Arrays类提供内置的asList()方法。

以下是它的语法:

Arrays.asList(array);

其中,“array”指的是数组引用。

多种方法

我们提供了不同的方法来解决这个问题。

  • 使用数组的静态初始化。

  • 使用用户自定义方法。

让我们逐一查看程序及其输出。

方法1:使用数组的静态初始化

示例

在这种方法中,数组元素将在程序中初始化。然后,根据算法查找两个数组的并集。

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the first array elements
      Integer arr1[] = {12, 13, 5, 16, 9, 19};
      
      //Declare and initialize the second array elements
      Integer arr2[] = {16, 2, 60, 9, 8, 5};
      
      //Initialize Hashset to perform union operation
      HashSet<Integer> set = new HashSet<>();
      
      //add first array to set
      set.addAll(Arrays.asList(arr1));
      
      //add second array to set
      set.addAll(Arrays.asList(arr2));
      
      //convert to array from set
      Integer[] union = {};
      union = set.toArray(union);
      
      //print the result
      System.out.println("Union of two arrays is: " + Arrays.toString(union));
   }
}

输出

Union of two arrays is: [16, 2, 19, 5, 8, 9, 12, 60, 13]

方法2:使用用户自定义方法

示例

在这种方法中,数组元素将在程序中初始化。然后,通过将数组作为参数传递给用户自定义方法,并在方法内部根据算法查找两个数组的并集。

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the first array elements
      Integer arr1[] = { 0, 5, 11, 7, 9, 3};
      
      //Declare and initialize the second array elements
      Integer arr2[] = { 1, 2, 4, 5, 12, 7 };
      union(arr1, arr2);
   }
   
   //user defined method
   public static void  union(Integer []arr1, Integer []arr2){
   
      //Initialize Hashset to perform union operation
      HashSet<Integer> set = new HashSet<>();
      
      //add first array to set
      set.addAll(Arrays.asList(arr1));
      
      //add second array to set
      set.addAll(Arrays.asList(arr2));
      
      //convert to array from set
      Integer[] union = {};
      union = set.toArray(union);
      
      //print the result
      System.out.println("Union of two arrays is: " + Arrays.toString(union));
   }
}

输出

Union of two arrays is: [0, 1, 2, 3, 4, 5, 7, 9, 11, 12]

在这篇文章中,我们探讨了如何在Java中查找两个数组的并集。

更新于:2023年1月5日

6K+ 次浏览

启动您的职业生涯

完成课程后获得认证

开始学习
广告
© . All rights reserved.