Java program to concatenate / join two array


This program will show you how to concatenate or join two arrays of different lengths.

Program
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int pos=0; System.out.println("Enter 1st array size"); int n1=sc.nextInt(); int arr1[]=new int[n1]; System.out.println("Enter 2nd array size"); int n2=sc.nextInt(); int arr2[]=new int[n2]; int n=n1+n2; int arr[]=new int[n]; System.out.println("Enter 1st array elements"); for(int i=0;i<n1;i++) { arr1[i]=sc.nextInt(); } System.out.println("Enter 2nd array elements"); for(int i=0;i<n2;i++) { arr2[i]=sc.nextInt(); } for (int num : arr1) { arr[pos] = num; pos++; } for (int num : arr2) { arr[pos] = num; pos++; } System.out.println(Arrays.toString(arr)); } }
Input
Output