Merge arrays into a new object array in Java

JavaObject Oriented ProgrammingProgramming

Following is the Java program to merge array into a new object array in Java −

Example

 Live Demo

import java.util.stream.Stream;
import java.util.Arrays;
import java.io.*;
public class Demo{
   public static <T> Object[] concat_fun(T[] my_obj_1, T[] my_obj_2){
      return Stream.concat(Arrays.stream(my_obj_1), Arrays.stream(my_obj_2)).toArray();
   }
   public static void main (String[] args){
      Integer[] my_obj_1 = new Integer[]{67, 83, 90};
      Integer[] my_obj_2 = new Integer[]{11, 0, 56};
      Object[] my_obj_3 = concat_fun(my_obj_1,my_obj_2);
      System.out.println("The two objects merged into a single object array : " +  Arrays.toString(my_obj_3));
   }
}

Output

The two objects merged into a single object array : [67, 83, 90, 11, 0, 56]

A class named Demo contains the ‘concat_fun’ that takes two objects and returns the concatenated objects as output. In the main string, two integer objects are created and a third object is used to store the concatenated objects. The relevant messages are displayed on the console.

raja
Published on 07-Jul-2020 11:49:44
Advertisements