Concatenate Arrays Java


Concatenate Arrays is very practical in real time environment. It is easy to copy one array into another, or clubbing both array elements. To make programming easy, the designers included arraycopy() static method in System class.
Concatenate Arrays: Following code concatenates two string arrays in three ways. Java comes with still many more ways using collection classes.
import java.util.*;
public class Concatenation
{
  public static void main(String args[])
  {                                               // two strings arrays
    String names[] = {"Rao", "Pillai", "Raman", "Hegde"};
    String states[] = {"Andhra", "Kerala", "Tamilnadu", "Karnataka"};

// first way, Java style using System.arraycopy() method

    String bigArray[] = new String[names.length+states.length];

    System.arraycopy(names, 0, bigArray, 0, names.length);
    System.arraycopy(states, 0, bigArray, names.length, states.length);

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

// second way, C/C++ style, round about process using String concat() method

    String s1="", s2="";
    for(String str : names)
    {
      s1 += str + ", ";
    }

    for(String str : states)
    {
      s2 += str + ", ";
    }
   
    String s3 = s1.concat(s2);
    System.out.println("\n\n"+s3);

// third way, using Collections framework classes, not preferred

    ArrayList al2 = new ArrayList();   
    Collections.addAll(al2, names);
    Collections.addAll(al2, states);
    System.out.println("\nArrayList elements with addAll(): " + al2);
		                                  // if desired, convert array list to array back
    Object mix[] = al2.toArray();
    System.out.print("\nmix elements: " );
    for(Object str : mix)
    {
      System.out.print(str + " : ");
    }
  }
}
Concatenate Arrays
Output screen of Concatenate Arrays

Two string arrays names and states are initialized with four string elements each. Now it is required to concatenate them into one array. states is copied into names. That is, names contains states elements also after concatenation. Here, we use the static method arraycopy() defined in System class. states.length gives the size of the array or length of the array; that is, number of elements present in the array.

System.arraycopy(names, 0, bigArray, 0, names.length);
System.arraycopy(states, 0, bigArray, names.length, states.length);

It may be confusing to see the arraycopy() parameters. Once you know the method signature, everything will be clear.

System.arraycopy(source array, position in the source array, destination array, position in the destination array, number of elements to copy);

source array: The array into which other array elements are to be copied
position in the source array: To what position in the source array the other array (destination array) elements are to be copied. You can prescribe the index number.
destination array: The array whose elements are to be copied
position in the destination array: From what position the elements are to be copied
number of elements to copy: Write the number of elements from destination array to copy.

From the above explanation of arraycopy(), understand the above two statements. It takes sometime to you. If still unable, pass a comment available at the end of the notes; you will get the replay next day.

The other way is each element of the array is read into a string. Two strings s1 and s2 contains the elements of names and states. The two strings s1 and s2 are concatenated to a third string, s3.

ArrayList al2 = new ArrayList();
Collections.addAll(al2, names);
Collections.addAll(al2, states);

An ArrayList object is al2 created and all the elements of the two arrays are added to ArrayList with Collections.addAll() method. java.util.Collections class comes with a number of methods to manipulate DS elements. Similarly, java.util.Arrays comes with many methods to operate on array elements.

java.util.Collections and java.util.Arrays are really two gifts gifted by the Designers to the programmers with which the programmers can forget the long laborious code of C/C++ to manipulate elements.

You can have: All Array Operations at a Glance

Leave a Comment

Your email address will not be published.