Java 中检查一个数组是否为另一个数组的子集


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

根据问题陈述,我们必须检查一个数组是否为另一个数组的子集。如果子数组的所有元素都存在于给定数组中,则该数组是另一个数组的子集。

让我们探索本文,看看如何使用 Java 编程语言来实现。

为您展示一些实例

实例 1

Suppose the original array is array1 {33, 51, 5, 31, 9, 4, 3}
The sub array is array2 {51, 9, 33, 3}
After checking if original array contains all elements of sub array, result will be:
array2 is a subset of array1

实例 2

Suppose the original array is array1 {14, 11, 33, 2, 9, 1}
The sub array is array2 {11, 2, 7, 1}
After checking if original array contains all elements of sub array, result will be:
array2 is not a subset of array1

实例 3

Suppose the original array is array1 {8, 28, 41, 3, 29, 10}
The sub array is array2 {28, 10}
Hence array2 is a sub array of array1

算法

算法 1(使用 2 个循环)

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

  • 步骤 2 - 实现多种方法的逻辑。

  • 步骤 3 - 初始化两个循环,并检查内循环的元素是否与外循环的元素匹配。

  • 步骤 4 - 打印结果。

算法 2(使用 HashList)

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

  • 步骤 2 - 实现多种方法的逻辑。

  • 步骤 3 - 初始化哈希集,并通过 “.contains(arr1[i])” 检查子数组的元素是否在原始数组中。

  • 步骤 4 - 打印结果。

算法 3(使用 List)

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

  • 步骤 2 - 实现多种方法的逻辑。

  • 步骤 3 - 初始化数组列表,并检查子数组元素是否在原始数组中。

  • 步骤 4 - 打印结果。

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

语法

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

以下是其语法:

array.length

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

多种方法

我们提供了不同方法的解决方案。

  • 使用 2 个循环

  • 使用哈希

  • 使用 List.contains() 方法

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

方法 1:使用 2 个循环

初始化两个循环,并检查内循环的元素是否与外循环的元素匹配。然后根据算法检查一个数组是否为另一个数组的子集。

示例

Open Compiler
public class Main { public static void main(String args[]) { int array1[] = { 33, 51, 5, 31, 9, 4, 3 }; int array2[] = { 51, 9, 33, 3 }; int x = array1.length; int y = array2.length;subset(array1, array2, x, y); if (subset(array1, array2, x, y)) { System.out.print("array 2 is a subset of array 1"); } else { System.out.print("array 2 is not a subset of array 1"); } } //user defined method to check if array 2 is present in array 1 static boolean subset(int array1[], int array2[], int x, int y) { int i, j = 0; for (i = 0; i < y; i++) { for (j = 0; j < x; j++) if (array2[i] == array1[j]) break; /* return false when arr2[i] is not present in arr1[] */ if (j == x) return false; } /* return true when all elements of arr2[] are present in arr1[] */ return true; } }

输出

array 2 is a subset of array 1

方法 2:使用哈希

初始化哈希集,并通过 “.contains(arr1[i])” 检查子数组的元素是否在原始数组中。然后根据算法检查一个数组是否为另一个数组的子集。

示例

Open Compiler
import java.util.HashSet; public class Main { public static void main(String[] args) { //declaring and initialising arrays int arr1[] = { 14, 11, 33, 2, 9, 1 }; int arr2[] = { 11, 2, 7, 1 }; //getting the length of the arrray int x = arr1.length; int y = arr2.length; if (subset(arr1, arr2, x, y)) System.out.println("array 2 is a subset of array 1 "); else System.out.println( "array 2 is not a subset of array 1"); } /* Return true if arr2[] is a subset of arr1[] */ static boolean subset(int arr1[], int arr2[], int x, int y) { //declaring hashset HashSet<Integer> hashset = new HashSet<>(); // hashset stores all the values of arr1 for (int i = 0; i < x; i++) { if (!hashset.contains(arr1[i])) hashset.add(arr1[i]); } // for loop to check if all elements of arr2 also lies in arr1 for (int i = 0; i < y; i++) { if (!hashset.contains(arr2[i])) /* return false when arr2[i] is not present in arr1[] */ return false; } /* return true when all elements of arr2[] are present in arr1[] */ return true; } }

输出

array 2 is not a subset of array 1

方法 3:使用 List.contains() 方法

初始化数组列表,并检查子数组元素是否在原始数组中。然后根据算法检查一个数组是否为另一个数组的子集。

示例

Open Compiler
import java.util.*; public class Main { public static void main(String[] args) { //declaring and initialising arrays Integer arr1[] = { 8, 28, 41, 3, 29, 10 }; Integer arr2[] = { 28, 10}; //printing the arrays System.out.println("Original array is " + Arrays.toString(arr1)); System.out.println("The sub array is " + Arrays.toString(arr2)); //converting array to array list List<Integer> arr = new ArrayList<Integer>(Arrays.asList(arr1)); // use contains() to check if the element 28 is present or not boolean ans = arr.contains(28); //if 28 is present then print successful message if (ans) System.out.println("The array 1 contains 28"); else System.out.println("The array 1 does not contains 28"); // use contains() to check if the element 10 is present or not ans = arr.contains(10); //if 10 is present then print successful message if (ans) System.out.println("The array 1 contains 10"); else System.out.println("The array 1 does not contains 10"); //print all elements of array 2 is in array 1 System.out.println("Hence array 2 is a sub array of array 1"); } }

输出

Original array is [8, 28, 41, 3, 29, 10]
The sub array is [28, 10]
The array 1 contains 28
The array 1 contains 10
Hence array 2 is a sub array of array 1

在本文中,我们探讨了如何使用 Java 编程语言检查一个数组是否为另一个数组的子集。

更新于: 2023 年 1 月 31 日

4K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告