0 votes
1 view
in Java by (9.5k points)

Below is the code I tried to combine the arrays {1, 2, 3} and {4, 5, 6} into {1, 2, 3, 4, 5, 6}. Can anyone tell me how to do it? 

public class combine {

  public static void main(String[]args){

  int[]a = {1, 2, 3};

  int[]b = {4, 5, 6};

  int[]c = new int[a+b];

  for(int i=0; i<a.length; i++)

  System.out.print(c[i]+" ");

}

public static int[]merge(int[]a, int[]b){

  int[]c = new int[a.length+b.length];

  int i;

  for(i=0; i<a.length; i++)

     c[i] = a[i];

     for(int j=0; j<b.length; j++)

        c[i++]=b[j];

        return c;

}

}

1 Answer

0 votes
by (19.7k points)

You have to use the merge and assign the result to an array like below: 

int[]c = merge(a,b);

Below code to loop for the array: 

int[]c = merge(a,b);

for(int i=0; i<c.length; i++)

    System.out.print(c[i]+" ");

Interested in Java? Check out this Java Certification by Intellipaat.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...