Javaarray Merging arrays
Directory (?) [+]
- A apache-commons
- Two systemarraycopy
- Three arrayscopyof
- Four arraynewinstance
Http://freewind.me/blog/20110922/350.html
In Java, how do you merge two String[]
into one?
It seems to be a very simple question. But how to write the code efficiently and concisely is still worth thinking about. Here are four methods, please refer to the selection.
First, Apache-commons
This is the simplest way. In Apache-commons, there is a ArrayUtils.addAll(Object[], Object[])
way to get our line done:
String[] both = (String[]) ArrayUtils.addAll(first, second);
Everything else needs to be packaged by invoking the methods provided in the JDK.
For convenience, I will define a tool method concat
that can combine two numbers together:
static String[] concat(String[] first, String[] second) {}
For general purposes, I will use generics to define it, where possible, so that not only String[]
can it be used, but other types of arrays can also be used:
static <T> T[] concat(T[] first, T[] second) {}
Of course, if your JDK doesn't support generics, or you can't use them, you can replace T with a manual String
.
Second, system.arraycopy ()
[Java]View Plaincopyprint?
- 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;
- }
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 the following:
String[] both = concat(first, second);
Third, arrays.copyof ()
In Java6, there is a method Arrays.copyOf()
that is a generic function. We can use it to write a more general method of merging:
[Java]View Plaincopyprint?
- 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;
- }
public static <T> t[] Concat (t[] First, t[] second) { t[] result = arrays.copyof (First, first.length + second.le Ngth); System.arraycopy (second, 0, result, first.length, second.length); return result;}
If you want to merge multiple, you can write this:
[Java]View Plaincopyprint?
- 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;
- }
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 the following:
String[] both = concat(first, second);String[] more = concat(first, second, third, fourth);
Iv. array.newinstance
You can also use Array.newInstance
to generate an array:
[Java]View Plaincopyprint?
- Private static <T> t[] Concat (t[] A, t[] b) {
- final int alen = a.length;
- final int blen = 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 + Blen);
- System.arraycopy (A, 0, result, 0, Alen);
- System.arraycopy (b, 0, result, Alen, Blen);
- return result;
- }
How to merge two arrays into one in Java