Friday, 18 July 2014

Combine Two Arrays

import com.google.common.collect.ObjectArrays;
import java.util.Arrays;
import org.apache.commons.lang.ArrayUtils;
/**
 * Java program to combine two arrays in Java. In this Java example
 * first we have concatenated two int arrays and later two String arrays.
 * First array combine examples uses Apache commons ArrayUtils, second array
 * join example uses Guava's ObjectArrays and last array concatenate example uses JDK.
 *
 * @author Javin Paul
 */
public class CombineArrayJava {
    public static void main(String args[]) {
      
        int [] first = {1,2,3, 4};
        int [] second = {5,6,7,8};
      
        // combine two arrays in Java using Apache commons ArrayUtils
        int [] combined = ArrayUtils.addAll(first, second);
     
        System.out.println("First array : " + Arrays.toString(first));
        System.out.println("Second array : " + Arrays.toString(second));
        System.out.println("Combined array : " + Arrays.toString(combined));
    
        String [] one = {"a", "b", "c"};
        String [] two = {"d", "e"};
      
        //joining array in Java using Guava library
        String [] joined = ObjectArrays.concat(one, two, String.class);
        System.out.println("Joined array : " + Arrays.toString(joined));
      
        //JDK way to combine two array in Java
        int[] array1 = {101,102,103,104};
        int[] array2 = {105,106,107,108};
        int[] concatenate = combine(array1, array2);
        System.out.println("concatenated array : " + Arrays.toString(concatenate));
      
    }
  
 
    public static int[] combine(int[] a, int[] b){
        int length = a.length + b.length;
        int[] result = new int[length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }
  
}
Output:
First array : [1, 2, 3, 4]
Second array : [5, 6, 7, 8]
Combined array : [1, 2, 3, 4, 5, 6, 7, 8]
Joined array : [a, b, c, d, e]
concatenated array : [101, 102, 103, 104, 105, 106, 107, 108]

No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...