How to merge two arrays into one in Java

Source: Internet
Author: User
Tags addall

Javaarray Merging arrays

Directory (?) [+]

    1. A apache-commons
    2. Two systemarraycopy
    3. Three arrayscopyof
    4. 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?
    1. Static string[] Concat (string[] A, string[] b) {
    2. String[] c= new string[a.length+b.length];
    3. System.arraycopy (A, 0, C, 0, a.length);
    4. System.arraycopy (b, 0, C, A.length, b.length);
    5. return C;
    6. }
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?
    1. Public static <T> t[] Concat (t[] First, t[] second) {
    2. T[] result = arrays.copyof (First, first.length + second.length);
    3. System.arraycopy (second, 0, result, first.length, second.length);
    4. return result;
    5. }
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?
  1. Public static <T> t[] Concatall (t[] First, t[] ... rest) {
  2. int totallength = first.length;
  3. For (t[] array:rest) {
  4. Totallength + = Array.Length;
  5. }
  6. T[] result = arrays.copyof (first, totallength);
  7. int offset = first.length;
  8. For (t[] array:rest) {
  9. System.arraycopy (Array, 0, result, offset, array.length);
  10. Offset + = Array.Length;
  11. }
  12. return result;
  13. }
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?
  1. Private static <T> t[] Concat (t[] A, t[] b) {
  2. final int alen = a.length;
  3. final int blen = b.length;
  4. if (alen = = 0) {
  5. return B;
  6. }
  7. if (Blen = = 0) {
  8. return A;
  9. }
  10. final t[] result = (t[]) Java.lang.reflect.Array.
  11. Newinstance (A.getclass (). Getcomponenttype (), Alen + Blen);
  12. System.arraycopy (A, 0, result, 0, Alen);
  13. System.arraycopy (b, 0, result, Alen, Blen);
  14. return result;
  15. }

How to merge two arrays into one in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.