Java Program to Concatenate Two Arrays

Java Program to Concatenate Two Arrays

What are Arrays?

In Java, an array is a container object that holds a fixed number of values of a single type. The values can be of any type, including primitive types (such as int, char, and boolean) and reference types (such as objects).

In this Article, we will write a program to calculate average of the values of an array.

Array Class:

In Java, the java.util.Arrays class is a utility class that provides various methods for working with arrays. Here are some examples of the methods that are available in the Arrays class:

  • sort: sorts the elements of an array in ascending order.
  • binarySearch: searches for a specified element in an array using binary search.
  • fill: fills an array with a specified value.
  • equals: checks if two arrays are equal.

Concatenation :

Concatenation is the process of joining two or more strings together to form a single string. It is often used to combine multiple pieces of text, such as words or sentences, into a larger whole.
In most programming languages, concatenation can be achieved by using the “+” operator. For example, in Java, you can concatenate two strings using the following syntax:

String s1 = "Hello";
String s2 = "World";
String s3 = s1 + " " + s2; // s3 will be "Hello World" 

Program to Concatenate Two Arrays using arraycopy() :

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing two sample arrays
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        
        // Initializing resultant array
        int[] result = new int[array1.length + array2.length];

        // Concatenate the two arrays into the result array
        System.arraycopy(array1, 0, result, 0, array1.length);
        System.arraycopy(array2, 0, result, array1.length, array2.length);

        // Print the result array
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

Output:

1 2 3 4 5 6

Explanation:

This program creates two arrays, array1 and array2, and adds some elements to them. Then it creates a new array, result, with a length that is the sum of the lengths of array1 and array2. The System.arraycopy method is used to copy the elements from array1 and array2 into the result array. Finally, it prints the elements of the result array.

The System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length) method is used to copy an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

This program concatenates the two arrays in the sense that it creates a new array that contains the elements of both arrays in the order they appear in the source arrays. It doesn’t change the original arrays.

Program to Concatenate Two Arrays using Iterations :

Run
import java.util.*;

public class Main{
    public static void main(String[] args){
        // Initializing two sample arrays
        int[] array1 = {1, 2, 3};
        int[] array2 = {4, 5, 6};
        
        // Initializing resultant array
        int[] result = new int[array1.length + array2.length];

        // Concatenate the two arrays into the result array
        int pos = 0;
        for (int element : array1) {
            result[pos] = element;
            pos++;
        }

        for (int element : array2) {
            result[pos] = element;
            pos++;
        }
        // Print the result array
        for (int i : result) {
            System.out.print(i + " ");
        }
    }
}

Output:

1 2 3 4 5 6

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription