This article describes the Java implementation method of combining two arrays into one. Share it for your reference, as follows:
In Java, how to merge two String[] into one?
It seems to be a very simple question. However, how to write the code efficiently and concisely is still worth thinking about. Here are four methods, please refer to the selection.
1. apache-commons
This is the easiest way. In apache-commons, there is an ArrayUtils.addAll(Object[], Object[]) method that allows us to do it in one line:
String[] both = (String[]) ArrayUtils.addAll(first, second);
The others need to call the methods provided in jdk by yourself and wrap them.
For convenience, I will define a tool method concat that can combine two arrays together:
static String[] concat(String[] first, String[] second) {}
For general purpose, I will use generics to define where possible so that not only String[] can be used, but other types of arrays can also be used:
static <T> T[] concat(T[] first, T[] second) {}
Of course, if your jdk does not support generics or cannot use it, you can manually change T to String.
2. System.arraycopy()
static String[] concat(String[] a, String[] b) { String[] c= new String[a.length+b.length]; System.arraycopy(a, 0, c, 0, a.length); System.arraycopy(b, 0, c, a.length, b.length); return c;}Use as follows:
String[] both = concat(first, second);
3. Arrays.copyOf()
In java6, there is a method Arrays.copyOf() , which is a generic function. We can use it to write a more general merge method:
public static <T> T[] concat(T[] first, T[] second) { T[] result = Arrays.copyOf(first, first.length + second.length); System.arraycopy(second, 0, result, first.length, second.length); return result;}If you want to merge multiple, you can write this:
public static <T> T[] concatAll(T[] first, T[]... rest) { int totalLength = first.length; for (T[] array : rest) { totalLength += array.length; } T[] result = Arrays.copyOf(first, totalLength); int offset = first.length; for (T[] array : rest) { System.arraycopy(array, 0, result, offset, array.length); offset += array.length; } return result;}Use as follows:
String[] both = concat(first, second);String[] more = concat(first, second, third, fourth);
4. Array.newInstance
You can also use Array.newInstance to generate an array:
private static <T> T[] concat(T[] a, T[] b) { final int alen = a.length; final int blend = b.length; if (alen == 0) { return b; } if (blen == 0) { return a; } final T[] result = (T[]) java.lang.reflect.Array. newInstance(a.getClass().getComponentType(), alen + blend); System.arraycopy(a, 0, result, 0, alen); System.arraycopy(b, 0, result, alen, blend); return result;}For more information about Java related content, please check out the topics of this site: "Summary of Java array operation skills", "Summary of Java characters and string operation skills", "Summary of Java mathematical operation skills", "Tutorial on Java Data Structures and Algorithms" and "Summary of Java Operation DOM Node Skills"
I hope this article will be helpful to everyone's Java programming.