Java program to concatenate two arrays using array copy method
Program
import java.util.Arrays;
import java.util.Scanner;
public class ConcatenateTwoArraysUsingarraycopy
{
public static void main(String[] args) {
int i;
System.out.println("Enter the required size of the first array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
int[] inputFirstArray = new int[size];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputFirstArray[i] = reader.nextInt();
}
System.out.println("Enter the required size of the second array: ");
size = reader.nextInt();
int[] inputSecondArray = new int[size];
System.out.println("Enter the elements of the array: ");
for (i = 0; i < size; i++) {
inputSecondArray[i] = reader.nextInt();
}
int firstLen = inputFirstArray.length;
int secondLen = inputSecondArray.length;
int[] mergedArray = new int[firstLen + secondLen];
System.arraycopy(inputFirstArray, 0, mergedArray, 0, firstLen);
System.arraycopy(inputSecondArray, 0, mergedArray, firstLen, secondLen);
System.out.println("Arrays after merging: ");
System.out.println(Arrays.toString(mergedArray));
}
}
This Java program concatenates two arrays using the System.arraycopy()
method.
-
Input First Array:
- The user is prompted to enter the size of the first array (
size
). - An integer array
inputFirstArray[]
is created with the specified size. - The user enters the elements, which are stored in
inputFirstArray
.
- The user is prompted to enter the size of the first array (
-
Input Second Array:
- The user is prompted to enter the size of the second array (
size
). - A second integer array
inputSecondArray[]
is created with the specified size. - The user enters the elements, which are stored in
inputSecondArray
.
- The user is prompted to enter the size of the second array (
-
Determine Array Sizes:
- The lengths of the two input arrays are stored in
firstLen
andsecondLen
.
- The lengths of the two input arrays are stored in
-
Concatenate Arrays:
- A new array
mergedArray[]
is created with a size equal to the sum of the lengths of the two input arrays. - The
System.arraycopy()
method is used to copy the elements of the first and second arrays intomergedArray
:System.arraycopy(inputFirstArray, 0, mergedArray, 0, firstLen)
:- Copies all elements from
inputFirstArray
starting at index0
intomergedArray
starting at index0
.
- Copies all elements from
System.arraycopy(inputSecondArray, 0, mergedArray, firstLen, secondLen)
:- Copies all elements from
inputSecondArray
starting at index0
intomergedArray
starting at indexfirstLen
.
- Copies all elements from
- A new array
-
Output:
- The
Arrays.toString()
method is used to print the contents of the concatenated array (mergedArray
).
- The
Example:
- Input Arrays:
[1, 2, 3]
and[4, 5, 6]
- Concatenated Array:
[1, 2, 3, 4, 5, 6]
Output
javac .\ConcatenateTwoArraysUsingarraycopy.java
java ConcatenateTwoArraysUsingarraycopy
Enter the required size of the first array:
3
Enter the elements of the array:
11
22
33
Enter the required size of the second array:
3
Enter the elements of the array:
44
55
66
Arrays after merging:
[11, 22, 33, 44, 55, 66]