How can I concatenate two arrays in Java?

后端 未结 30 2028
走了就别回头了
走了就别回头了 2020-11-21 06:05

I need to concatenate two String arrays in Java.

void f(String[] first, String[] second) {
    String[] both = ???
}

What is t

相关标签:
30条回答
  • I've recently fought problems with excessive memory rotation. If a and/or b are known to be commonly empty, here is another adaption of silvertab's code (generified too):

    private static <T> T[] concatOrReturnSame(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;
    }
    

    Edit: A previous version of this post stated that array re-usage like this shall be clearly documented. As Maarten points out in the comments it would in general be better to just remove the if statements, thus voiding the need for having documentation. But then again, those if statements were the whole point of this particular optimization in the first place. I'll leave this answer here, but be wary!

    0 讨论(0)
  • 2020-11-21 06:46

    Using the Java API:

    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[both.size()]);
    }
    
    0 讨论(0)
  • 2020-11-21 06:46
    ArrayList<String> both = new ArrayList(Arrays.asList(first));
    both.addAll(Arrays.asList(second));
    
    both.toArray(new String[0]);
    
    0 讨论(0)
  • 2020-11-21 06:48

    Using only Javas own API:

    
    String[] join(String[]... arrays) {
      // calculate size of target array
      int size = 0;
      for (String[] array : arrays) {
        size += array.length;
      }
    
      // create list of appropriate size
      java.util.List list = new java.util.ArrayList(size);
    
      // add arrays
      for (String[] array : arrays) {
        list.addAll(java.util.Arrays.asList(array));
      }
    
      // create and return final array
      return list.toArray(new String[size]);
    }
    

    Now, this code ist not the most efficient, but it relies only on standard java classes and is easy to understand. It works for any number of String[] (even zero arrays).

    0 讨论(0)
  • 2020-11-21 06:50

    Here a possible implementation in working code of the pseudo code solution written by silvertab.

    Thanks silvertab!

    public class Array {
    
       public static <T> T[] concat(T[] a, T[] b, ArrayBuilderI<T> builder) {
          T[] c = builder.build(a.length + b.length);
          System.arraycopy(a, 0, c, 0, a.length);
          System.arraycopy(b, 0, c, a.length, b.length);
          return c;
       }
    }
    

    Following next is the builder interface.

    Note: A builder is necessary because in java it is not possible to do

    new T[size]

    due to generic type erasure:

    public interface ArrayBuilderI<T> {
    
       public T[] build(int size);
    }
    

    Here a concrete builder implementing the interface, building a Integer array:

    public class IntegerArrayBuilder implements ArrayBuilderI<Integer> {
    
       @Override
       public Integer[] build(int size) {
          return new Integer[size];
       }
    }
    

    And finally the application / test:

    @Test
    public class ArrayTest {
    
       public void array_concatenation() {
          Integer a[] = new Integer[]{0,1};
          Integer b[] = new Integer[]{2,3};
          Integer c[] = Array.concat(a, b, new IntegerArrayBuilder());
          assertEquals(4, c.length);
          assertEquals(0, (int)c[0]);
          assertEquals(1, (int)c[1]);
          assertEquals(2, (int)c[2]);
          assertEquals(3, (int)c[3]);
       }
    }
    
    0 讨论(0)
  • 2020-11-21 06:51

    How about simply

    public static class Array {
    
        public static <T> T[] concat(T[]... arrays) {
            ArrayList<T> al = new ArrayList<T>();
            for (T[] one : arrays)
                Collections.addAll(al, one);
            return (T[]) al.toArray(arrays[0].clone());
        }
    }
    

    And just do Array.concat(arr1, arr2). As long as arr1 and arr2 are of the same type, this will give you another array of the same type containing both arrays.

    0 讨论(0)
提交回复
热议问题