Thursday, 18 October 2012

How to merge two arrays


//Sample Example of how to merge two different array.

package com.gaurav.arrayexamples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ArrayMerge {

 public static void main(String args[]){
  String first[] = {"Kumar","Amit","Bimal"};
  String second[] = {"Shivam","Gaurav"};
  List<String> lst = new ArrayList<String>();
  lst.addAll(Arrays.asList(first));
  lst.addAll(Arrays.asList(second));

  Collections.sort(lst);

  Object [] third = lst.toArray();
  for(Object str: third){
   System.out.println("Elements are-"+str);
  }


 }
}

No comments:

Post a Comment