Java程式合併兩個陣列


在本文中,使用者將合併兩個陣列。使用者建立任何型別的兩個陣列作為輸入,例如 arr1[] 和 arr2[],並提供示例。使用者必須建立包含多個元素的兩種型別的陣列作為輸入,並以不同元素作為輸出呈現結果。使用 Java 程式語言以排序順序列印陣列的所有元素。本文說明了兩種方法。第一種方法使用陣列的概念,而第二種方法使用 Map 函式來合併兩個陣列。使用 Map 函式的好處是可以檢索不包含重複值的最終陣列。

讓我們深入探討這篇文章,瞭解如何使用 Java 程式語言來實現。

為您展示一些例項

例項 1

假設陣列如下所示 -

I/P: arr1[]={2,1,8,5,7}

I/P: arr2[]={9,6,6,3,1}

O/P: arr3[]={1,1,2,3,5,6,6,7,8,9}

例項 2

假設陣列如下所示 -

I/P: arr3[]={8,8,0,6,6}

I/P: arr4[]={7,7,0,0,4}

O/P: arr3[]={4,6,6,7,7,8,8,0,0,0} 

語法

  • mergeArrays() - 此型別的方法用於合併陣列並將結果作為第三個陣列列印

  • Arrays.sort() - 此型別函式用於打印合並後的陣列並對包含在兩個陣列中的專案進行排序。

多種方法

使用者將兩種方法劃分為以下幾種 -

  • 使用樸素方法

  • 使用對映

讓我們逐一展示程式及其輸出。

注意 - 這些程式可能無法在任何線上 Java 編譯器上提供預期的結果。線上編輯器可能無法訪問您本地組織的檔案跟蹤。

方法 1:使用樸素方法

使用者可以顯示兩個陣列中的一個數組,該陣列應按順序合併。逐個建立兩個陣列的專案,並將結果列印為陣列 3。

讓我們展示程式及其輸出。

演算法

  • 步驟 1 - 開始

  • 步驟 2 - 宣告兩個陣列

  • 步驟 3 - 將元素儲存到陣列中

  • 步驟 4 - 獲取變數 v 並觀察陣列的長度

  • 步驟 5 - 使用 mergeArrays() 函式,該函式可以從變數 v 中合併陣列並將資料與兩個數組合並

  • 步驟 6 - 列印稱為輸出的第三個陣列

  • 步驟 7 - 列印結果

示例

import java.util.*;
//Take a class called Main
public class Main{
   // define main function
   public static void main(String[] args){
      //declare array
      int array1[]={1,5,4,8};
      //Obtain the length of the array
      int v1=array1.length;
      
      int array2[]={2,8,9,0};
      
      int v2=array2.length;
      //Merge the two arrays one by one into another array and print it
      int array3[]=new int[v1+v2];
      //Use the function mergeArrays and insert the variables of the values
      mergeArrays(array1, array2, v1, v2, array3);
   
      System.out.println("after merging the two arrays");
      // using for loop and go to the merging 
      for(int w=0;w<v1+v2;w++)
      //Print the third array with merged data
      System.out.print(array3[w]+" ");
   }
   // define the method mergeArrays
   public static void mergeArrays(int[] array1, int[] array2, int v1, int v2, int[] array3){
   
      int w=0;
      int b=0;
      int u=0;
      // take a while loop where v1 is greater than w
      while(w<v1){
         array3[u++]=array1[w++];
      }
      //using while loop
      while(b<v2){
         array3[u++]=array2[b++];
      
      }
      Arrays.sort(array3);
   }
}

輸出

After merging the two arrays
0 1 2 4 5 8 8 9

方法 2:使用對映

在這種方法中,使用者使用對映將合併後的陣列作為輸出列印。透過使用對映,將陣列 1 和陣列 2 的兩個陣列的元素作為輸入新增。使用者可以列印對映的輸入。使用者插入了 Java 庫並從 Java 包 java.io 中匯入類。

演算法

  • 步驟 1 - 開始

  • 步驟 2 - 宣告兩個陣列。

  • 步驟 3 - 將元素儲存在陣列中。

  • 步驟 4 - 獲取變數 o,然後使用 for 迴圈轉到陣列的長度。

  • 步驟 5 - 使用 mergeArrays() 函式,該函式可以從變數 v 中合併陣列並將資料與兩個數組合並。

  • 步驟 6 - 使用 Map 方法合併陣列。

  • 步驟 7 - 列印稱為輸出的第三個陣列。

  • 步驟 8 - 列印結果。

示例

// import the java libraries
import java.io.*;
import java.util.*;
// take a class called Main
public class Main{
   //Define the method mergeArrays to merge the arrays
   public static void mergeArrays(int a[], int b[], int n, int m){
      Map<Integer, Boolean>mw=new TreeMap<Integer, Boolean>();
      // using for loop
      for(int o=0;o<n; o++){
         mw.put(a[o], true);
   
      }
      //using for loop
      for(int o=0;o<m; o++){
         mw.put(b[o], true);
      }
      for(Map.Entry<Integer,Boolean>me: mw.entrySet()){
         // print the input
         System.out.print(me.getKey()+" ");
      }
   }
   //Define main function
   public static void main(String[] args){
      //Create the arrays one by one
      int a[]={2,3,6,8};
      int b[]={7,9,4,1};
      //Take the length of one array
      int sz=a.length;
      // take the length of another array
      int sz1=b.length;
      mergeArrays(a, b, sz, sz1);
   }
}

輸出

1 2 3 4 6 7 8 9

結論

在本文中,使用者探索了使用 Java 程式語言將兩種型別的陣列作為輸入合併的方法。使用者使用 Java 庫物理地合併兩個陣列。使用者重述陣列以提取資料並按順序將其複製到另一個數組中。

更新於: 2023-11-21

701 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告