How to copy two arrays into a single array with out custom function in java?

Answer: Following is the program to copy the multiple arrays into single array:

public MergeArray{

public static void main(String args[]){

int[] arr1 = {1,2,3,4,5};

int[] arr2 = {6,7,8,9,10};

int lenCopy = arr1.length+arr2.length;

int[] arrCopy = new int[lenCopy];

int count = 0;

for(int num:arr1){

arrCopy[count++] = num;

}

for(int num:arr2){

arrCopy[count++] = num;

}

}

}

Explanation: We initialized two arrays, arr1 and arr2, and calculated their combined length, storing it in the lenCopy variable.

Next, we created a third array, arrCopy, with a length equal to lenCopy.

Using two enhanced for-loops, we copied all elements from arr1 and arr2 into arrCopy.

As a result, arrCopy contains all the elements from both arrays, effectively merging them into a single array.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
0
Would love your thoughts, please comment.x
()
x