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(使用两个for循环)
- 步骤1 - 声明并初始化一个整数数组。
-
步骤2 - 实现多种方法的逻辑。
-
步骤3 - 初始化两个for循环,并检查内部循环的元素是否与外部循环的元素匹配。
-
步骤4 - 打印结果。
步骤-2(使用HashList)
-
步骤1 - 声明并初始化一个整数数组。
-
步骤2 - 实现多种方法的逻辑。
-
步骤3 - 初始化hashset,并使用”.contains(arr1[i])”检查子数组的元素是否在原始数组中。
-
步骤4 - 打印结果。
步骤-3(使用List)
-
步骤1 - 声明并初始化一个整数数组。
-
步骤2 - 实现多种方法的逻辑。
-
步骤3 - 初始化数组列表,并检查原始数组中是否存在子数组元素。
-
步骤4 - 打印结果。
语法
要获取数组的长度(即数组中的元素数量),数组有一个内置的属性,即 length 。
以下是它的语法:
array.length
其中,’array’是指数组引用。
多种方法
我们提供了不同的方法来解决问题。
- 使用两个for循环
-
使用哈希
-
使用List.contains()方法
让我们逐个查看程序及其输出。
方法1:使用两个for循环
初始化两个for循环,并检查内部for循环的元素是否与外部for循环的元素匹配。然后根据算法检查一个数组是否是另一个数组的子集。
示例
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])”来检查子数组的元素是否在原数组中。然后按照算法检查一个数组是否是另一个数组的子集。
示例
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()方法
初始化数组列表,并检查子数组元素是否存在于原始数组中。然后根据算法检查一个数组是否是另一个数组的子集。
示例
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编程语言探讨了如何检查一个数组是否是另一个数组的子集。