Java Join concat Arrays

Java Join concat Arrays


Java Join concat Arrays

Summary: By the end of this tutorial Java "Join concat Arrays", you will be comfortable to concatenate two Java arrays. Explained in simple terms, example code and screenshot for a Beginner.

There is no direct method in Java to join arrays (but there is a method to join two strings, concat()). 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 user-designed to concatenate two arrays.

Program on Java Join concat Arrays

Java Join concat Arrays

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

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

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.

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

Another style of Java Join concat Arrays using arraycopy() method

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

Can you answer these questions?

Array related topics that increase array manipulation techniques

3 thoughts on “Java Join concat Arrays

  1. Joseph

    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. Joseph

    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.

    1. S. Nageswara Rao, Corporate Trainer Post author

      import java.util.Arrays;

      public class Demo
      {
      public static void main (String args[])
      {
      String A[] = { “Joseph”, “Ariana”, “Xena”, “Rene”, “Peter”, “Diana”, “Rihanna”};
      String B[] = { “Madona”, “Diddy”, “Joe”, “Alice” };

      // first combine both arryays using arraycopy() method

      String C[] = new String[A.length + B.length];
      System.arraycopy(A, 0, C, 0, A.length);
      System.arraycopy(B, 0, C, A.length, B.length);

      // to know whether combined or not
      System.out.println(Arrays.toString(C));
      }
      }

      Comparable interface comes with compareTo() method.

      To get command over compareTo() refer these links;

      http://way2java.com/collections/interface-comparable/

      http://way2java.com/collections/comparable-example/

      http://way2java.com/collections/comparable-vs-comparator/

      Do some work, find out a way.

Leave a Reply

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