Monday, March 28, 2011

Add two Arrays


Adding two arrays in java, 
Using Commons lib (but you require commons-lang.jar file)
//import org.apache.commons.lang.ArrayUtils;
String[]first = new String[]{"1","2"};
String[]second = new String[]{"1","2"};
String[] both = (String[]) ArrayUtils.addAll(first, second);
Using JAVA API only
String[] f(String[] first, String[] second) {
    List<String> both = new ArrayList<String>(first.length + second.length);
    Collections.addAll(both, first);
    Collections.addAll(both, second);
    return both.toArray(new String[] {});
}

No comments:

Post a Comment