How can I concatenate two arrays in java

Java 8Object Oriented ProgrammingProgramming

Following program shows how to concatenate two arrays.

Example

public class Tester {
   public static void main(String[] args) {
      int[] a = {1,2,3};
      int[] b = {4,5};
      int mergeSize = a.length + b.length;
      int[] c = new int[mergeSize];
      int i =0;
     
      while(i < a.length) {
         c[i] = a[i];
         i++;
      }
      i = 0;
      while(i < b.length) {
         c[i + a.length] = b[i];
         i++;
      }
      i = 0;
      while(i < c.length) {
         System.out.println(c[i]);
         i++;
      }
   }
}
raja
Published on 06-Feb-2018 12:50:40
Advertisements