Joining (concatenating) Arrays


There is no direct method in Java to join arrays. You write the code yourselves. Following program writes a method that can join arrays; here an example of string arrays is taken. joinArrays() method is designed to concat two arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class StringArrayManipulations
{
  public static String[] joinArrays(String names1[], String names2[])
   {
     String result[] = new String[names1.length + names2.length];
 
     int i = 0;
     for(; i <names1.length; i++)
     {
       result[i] = names1[i];
     }
     for(int k = 0; k < names2.length; i++, k++)
     {
       result[i] = names2[k ];
     }
     return result;
   }
   public static void main(String args[])
   {
     String students[] = { "Jyosthi", "Jyostna", "Sridhar" };           
     String teachers[] = { "S N Rao", "Chandu", "Srinivas" };     
     String overall[] = joinArrays(students, teachers);
     for(String str : overall)
     {
       System.out.print(str + ", ");
     }      
   }
}

Two string arrays of students and teachers are initialized and passed to joinArrays() method.

       public static String[] joinArrays(String names1[], String names2[])

Observe, the joinArrays() takes two string array objects as parameters and values for these are passed from main() method.

       String result[] = new String[names1.length + names2.length];

A new string array result is created with the size of both the arrays names1 and names2. In two for loops elements of both arrays are copied into the new array result.

       for(String str : overall)
       {
         System.out.print(str + ", ");
       }

An enhanced for loop, introduced with JDK 1.5, is used to print the merged array elements.

Another way is using arraycopy() method shown in the next program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class StringArrayManipulations
{
  public static void joinArrays(String names1[], String names2[])
  {
    String result[] = new String[names1.length + names2.length];
 
    System.arraycopy(names1, 0, result, 0, names1.length);
 
    System.arraycopy(names2, 0, result, names1.length, names2.length);
 
    for(String str : result)
    {
      System.out.print(str + ", ");
    }      
  }
  public static void main(String args[])
  {
    String students[] = { "Jyosthi", "Jyostna", "Sridhar" };           
    String teachers[] = { "S N Rao", "Chandu", "Srinivas" };     
 
    joinArrays(students, teachers);
  }
}

For syntax of arraycopy(), refer "Java Array Copying".

Can you answer these questions?

Array related topics that increase array manipulation techniques

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  1. I have tried this way, but i can not succeeded.

    public class Opdracht1 {

    /**
    * @param args the command line arguments
    */

    public static void main(String[] args) {

    String A[] = { “Joseph”,”Ariana”,”Xena”,”Rene”,”Peter”,”Diana”,
    “Rihanna”};
    String B[] = {“Madona”,”Diddy”,”Joe”,”Alice”};

    for(int j = 0; j < A.length; j++) {
    for(int i = j + 1; i < A.length; i++) {
    if(A[i].compareToIgnoreCase(A[j]) < 0) {
    String K = A[j];
    A[j] = A[i];
    A[i] = K;
    }
    }

    List list = new ArrayList(Arrays.asList(A));
    list.addAll(Arrays.asList(B));
    Object[] C = list.toArray();

    System.out.println(Arrays.toString(C));

    }
    }
    }

  2. public static void main(String[] args) {

    String A[] = { “Joseph”,”Ariana”,”Xena”,”Rene”,”Peter”,”Diana”,
    “Rihanna”};
    String B[] = {“Madona”,”Diddy”,”Joe”,”Alice”};

    for(int j = 0; j < A.length; j++) {
    for(int i = 0 ; i < B.length; i++) {
    if(B[i].compareToIgnoreCase(A[j]) < 0) {
    String K = A[j];
    A[j] = B[i];
    B[i] = K;
    }
    }
    System.out.println(A[j]);

    }

    }
    }

    how to get both arrays combined and sorted using comapreTo method.