Contact@itspring.com
  • Java Program To Merge Two Arrays.

    In this post you will learn how to merge two arrays in Java.

    This code can be written in 3 different ways, let's see them one by one.
     
    1. Copy contents of both the arrays in new array :
     
    Here first you need to calculate the length of both the arrays and create a new array with size equal to the sum of lengths of the arrays to be merged. Once a new array is created, just copy the elements of both the arrays in new array using for loop.
     
    Note : Here you need to have a count variable, which will store index for newly created array. So that once all the elements of first array are copied, count will add the next element to the next index and not to the 0th index.
     
    Here is the code :

    <p>&nbsp;/*Java program to merge two arrays*/</p>
    <
    div>public class MergerArraysDemo {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">    </span>public static void main(String[] args) {</div>
    <
    div><span style="white-space:pre">        </span>// TODO Auto-generated method stub</div>
    <div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>//arrays to be merged</div>
    <div><span style="white-space:pre">        </span>int[] arr = { 1222};</div>
    <
    div><span style="white-space:pre">        </span>int[] arr2 = { 10445689};</div>
    <
    div><span style="white-space:pre">        </span>int count 0;</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// length of new array</div>
    <div><span style="white-space:pre">        </span>int length = (arr.length arr2.length);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// create new array</div>
    <div><span style="white-space:pre">        </span>int[] result = new int[length];</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// copy values from array1</div>
    <div><span style="white-space:pre">        </span>for (int i 0&ltarr.lengthi++) {</div>
    <
    div><span style="white-space:pre">            </span>result[i] = arr[i];</div>
    <
    div><span style="white-space:pre">            </span>count++;</div>
    <
    div><span style="white-space:pre">        </span>}</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// copy values from array2</div>
    <div><span style="white-space:pre">        </span>for (int j 0&ltarr2.lengthj++) {</div>
    <
    div><span style="white-space:pre">            </span>result[count++] = arr2[j];</div>
    <
    div><span style="white-space:pre">        </span>}<span style="white-space:pre">        </span></div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// print the merged arrays</div>
    <div><span style="white-space:pre">        </span>System.out.print(&quot;[&quot;);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>for (int i 0&ltresult.lengthi++) {</div>
    <
    div><span style="white-space:pre">            </span>System.out.print(&quot; &quot; + result[i] + &quot; &quot;);</div>
    <
    div><span style="white-space:pre">        </span>}</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>System.out.println(&quot;]&quot;);</div>
    <
    div><span style="white-space:pre">    </span>}</div>
    <
    div>&nbsp;</div>
    <
    div>}</div>

Most Liked Posts

Post A Comment

Your email address will not be published. Required fields are marked*

TZWV

0 Comments

No Record found
demo content

The Total Economic Impactâ„¢ of ServiceNow

United States Tue Jun 06 2017

Join us for a webinar where we will reveal the quantified value of the Lightspeed Enterprise for customers that are modernizing IT, eliminating service outages, and optimizing performance with real-time analytics.

Guest speakers Liz Witherspoon of Forrester, and study participant Brad Paubel, VP of Internal Technologies at Maritz, will discuss

  • The value drivers for IT and key quantified economic benefits
  • Key challenges that study customers faced and overcame
  • Focus areas and best practices you can use to drive business impact with ServiceNow

Register

Webinar Registration

Enter the characters visible in the box below

R4RW

Share

Share using

Advertise With Us