5

I've seen similar questions and none provide the answer that I'm looking for, so I apologize in advance if this is considered a duplicate. I'm trying to combine arrays {1, 2, 3} and {4, 5, 6} into {1, 2, 3, 4, 5, 6}. What am I doing incorrectly? I'm super new to java. Sorry if the question is stupid.

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;
}
}
Jeremy Stone
  • 350
  • 4
  • 12
  • 29

12 Answers12

9

Don't do it yourself, use System.arrayCopy() to copy both arrays into a new array of the combined size. That's much more efficient, as it uses native OS code.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
6

Instead of

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

You need to call your merge method and assign the result to the array like :

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

Also you for loop should be :

int[]c = merge(a,b);
for(int i=0; i<c.length; i++)
    System.out.print(c[i]+" ");
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
5
  String a[] = { "A", "E", "I" };
  String b[] = { "O", "U" };
  List list = new ArrayList(Arrays.asList(a));
  list.addAll(Arrays.asList(b));
  Object[] c = list.toArray();
  System.out.println(Arrays.toString(c));
Karan singh
  • 51
  • 1
  • 1
  • 2
    I know the code is pretty clear but it's always best to provide explanation of what you're doing. Plain code with no explanation tends to get flagged for deletion around here. Also - welcome to SO – StormeHawke Dec 18 '14 at 14:09
  • Thanks @StormeHawke , I am new to this community....will take care in future.... the code was self explanatory so didn't provide comment for it. – Karan singh Dec 26 '14 at 10:21
3

Please try this code, I hope it's useful for you

String a[] = new String[4];
    String b[] = new String[2];
    String[] ab = new String[a.length + b.length];
    int i, j, d, s = 0;
    @SuppressWarnings("resource")
    Scanner x = new Scanner(System.in);
    System.out.println("Enter the first array");

    for (i = 0; i < a.length; i++) {
        a[i] = x.next();
        for (d = i; d < a.length; d++) {
            ab[d] = a[i];
        }
    }

    System.out.println("Enter the second array");

    for (j = 0; j < b.length; j++) {
        b[j] = x.next();
        for (d = a.length + j; d < ab.length; d++)
            ab[d] = b[j];
    }
    System.out.println();
    System.out.println("The new array is !!");
    System.out.println("--------------------");
    for (s = 0; s < ab.length; s++) {
        System.out.print(ab[s] + " ");
    }
zx485
  • 28,498
  • 28
  • 50
  • 59
1
class MerginTwoArray{
public static void mergingarr(int x[], int y[])
{
    int len=x.length+y.length;
    int arr[]=new int[len];
    //create a variable j which will begin zeroth index of second array
    int j=0;
    for(int i=0; i<arr.length; i++)
    {
        if(i<x.length)
        {
            arr[i]=x[i];
        }
        else
        {
            arr[i]=y[j];
            j++;
        }
    }
    for(int i:arr)
    {
        System.out.print(i+ " ");
    }
}
public static void main(String... args)
{
    mergingarr(new int[]{1,2,3}, new int[]{4,5,6});
}

}

Hope this is clear to you

Ijhar Ansari
  • 310
  • 1
  • 2
  • 9
1

One more way of concatenating 2 arrays is:

System.out.println("Enter elements for a: ");
for (int i = 0; i < 5; i++) {
    int num = in.nextInt();
    a[i] = num;
}

System.out.println("Enter elements for b: ");
for (int i = 0; i < 5; i++) {
    int num = in.nextInt();
    b[i] = num;
}

void merge() {
    int c[] = new int[10];
    System.arraycopy(a, 0, c, 0, a.length);
    System.arraycopy(b, 0, c, a.length, b.length);

    System.out.println(Arrays.toString(c));  // merged array 
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Mahen Gandhi
  • 351
  • 2
  • 8
0

You can use the following:

package array;

public class Combine {

public static void main(String[] args) {

 int[]a = {1,2,3,4};
 int[]b = {4,16,1,2,3,22};
 int[]c = new int[a.length+b.length];
 int count=0;

  for(int i=0; i<a.length; i++)
    {
       c[i]=a[i];
       count++;
    }

 for(int j=0;j<b.length;j++)
    {
       c[count++]=b[j];
    }

        for(int i=0;i<c.length;i++)
        System.out.print(c[i]+" ");
}

}
PeterSW
  • 4,921
  • 1
  • 24
  • 35
0

Have a look at my solution (you can eventually sort it if need be):

public static int[] mergeAndSortIntArrays(int[] firstInt, int[] secondInt){

    List<Integer> merged = new ArrayList<>();

    for (int i=0; i<firstInt.length; i++){
        merged.add(firstInt[i]);
    }

    for (int i=0; i<secondInt.length; i++){
        merged.add(secondInt[i]);
    }

    Collections.sort(merged);
    int[] result=new int[merged.size()];
    for (int i=0; i<merged.size(); i++){
        result[i]=merged.get(i);
    }
    return result;
}
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
0
   int a[] = {
        5, 10, 15, 25
    };
    int b[] = {
        12, 5, 7, 9
    };
    int c[] = new int[8];
    for (int i = 0; i < 4; i++) {
        System.out.println(a[i]);

    }
    System.out.println("**************");
    for (int j = 0; j < 4; j++) {
        System.out.println(b[j]);
    }
    //int[] c=merge(a,b);
    for (int i = 0; i < 4; i++) {

        c[i] = a[i];

    }
    for (int i = 0; i < 4; i++) {
        for (int k = 4; k < 8; k++) {
            c[k] = b[i];
        }

    }
    for (int i = 0; i < 4; i++) {
        System.out.println(c[i]);
    }
0

Merge two array without arraylist.

    public class Main {


    static int a[] = {1, 2, 3, 4};
    static int b[] = {5, 6, 7, 8};

    public static void main(String[] args) {

        System.out.println("Hello World!");
        int totalLengh = a.length + b.length;

        int c[] = new int[totalLengh];

        int j = 0;
        for (int i = 0; i < totalLengh; i++) {

            if (i < a.length) {

                c[i] = a[i];

            } else {
                c[i] = b[j];
                j++;
            }

            System.out.println("" + c[i]);
        }
    }
}
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
-1
 public class MergeArrays {
    public static void main(String[]args){
         int[] a = {1, 2, 3};
         int[] b = {4, 5, 6};
         int[] c = new int[a.length+b.length];// Here length of int[] c will be 6
         int count = 0;

         //looping to store the value length of i
         for(int i = 0; i<a.length; i++) { 
             c[i] = a[i];
             count++;
          }
        //looping to store the value length of j
          for(int j = 0;j<b.length;j++) { 
             c[count++] = b[j];
          }
        //looping to retrieve the value of c
          for(int i = 0;i<c.length;i++) 
              System.out.print(c[i]);// Displaying looped value/output in single line at console 

    }
}
-3
public static void main(String[]args){

        int[]a = {1, 2, 3};
        int[]b = {4, 5, 6};
        int[]c ;
        c=merge(a,b);
        for(int i=0; i<c.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];
        System.out.println(i);
        for(int j=0; j<b.length; j++)
            c[i++]=b[j];
        return c;
    }