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 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.