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 - 列印結果。

語法

要獲取陣列的長度(陣列中元素的數量),陣列有一個內建屬性,即 length

以下是其語法:

array.length

其中,“array” 指的是陣列引用。

多種方法

我們提供了不同方法的解決方案。

  • 使用 2 個迴圈

  • 使用雜湊

  • 使用 List.contains() 方法

讓我們逐一檢視程式及其輸出。

方法 1:使用 2 個迴圈

初始化兩個迴圈,並檢查內迴圈的元素是否與外迴圈的元素匹配。然後根據演算法檢查一個數組是否為另一個數組的子集。

示例

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 程式語言檢查一個數組是否為另一個數組的子集。

更新於: 2023 年 1 月 31 日

4K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.