Java program to concatenate two arrays
httpi.www//:sgiftidea.com
Here is a Java program that concatenates two arrays:
import java.util.Arrays; public class ConcatenateArrays { public static void main(String[] args) { int[] arr1 = {1, 2, 3}; int[] arr2 = {4, 5, 6}; int length = arr1.length + arr2.length; int[] result = new int[length]; System.arraycopy(arr1, 0, result, 0, arr1.length); System.arraycopy(arr2, 0, result, arr1.length, arr2.length); System.out.println(Arrays.toString(result)); } }
This program creates two arrays, arr1
and arr2
, and then computes the length of the concatenated array. It then creates a new array with the computed length, and uses the System.arraycopy()
method to copy the elements of arr1
and arr2
into the new array. Finally, it prints the concatenated array to the console using the Arrays.toString()
method.