How can I concatenate two arrays in Java?

后端 未结 30 1959
走了就别回头了
走了就别回头了 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条回答
  • 2020-11-21 06:24

    Using Stream in Java 8:

    String[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b))
                          .toArray(String[]::new);
    

    Or like this, using flatMap:

    String[] both = Stream.of(a, b).flatMap(Stream::of)
                          .toArray(String[]::new);
    

    To do this for a generic type you have to use reflection:

    @SuppressWarnings("unchecked")
    T[] both = Stream.concat(Arrays.stream(a), Arrays.stream(b)).toArray(
        size -> (T[]) Array.newInstance(a.getClass().getComponentType(), size));
    
    0 讨论(0)
  • 2020-11-21 06:24

    You could try converting it into a Arraylist and use the addAll method then convert back to an array.

    List list = new ArrayList(Arrays.asList(first));
      list.addAll(Arrays.asList(second));
      String[] both = list.toArray();
    
    0 讨论(0)
  • 2020-11-21 06:27

    The Functional Java library has an array wrapper class that equips arrays with handy methods like concatenation.

    import static fj.data.Array.array;
    

    ...and then

    Array<String> both = array(first).append(array(second));
    

    To get the unwrapped array back out, call

    String[] s = both.array();
    
    0 讨论(0)
  • 2020-11-21 06:27

    Here's an adaptation of silvertab's solution, with generics retrofitted:

    static <T> T[] concat(T[] a, T[] b) {
        final int alen = a.length;
        final int blen = b.length;
        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;
    }
    

    NOTE: See Joachim's answer for a Java 6 solution. Not only does it eliminate the warning; it's also shorter, more efficient and easier to read!

    0 讨论(0)
  • 2020-11-21 06:27
    public String[] concat(String[]... arrays)
    {
        int length = 0;
        for (String[] array : arrays) {
            length += array.length;
        }
        String[] result = new String[length];
        int destPos = 0;
        for (String[] array : arrays) {
            System.arraycopy(array, 0, result, destPos, array.length);
            destPos += array.length;
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-11-21 06:27

    Another way to think about the question. To concatenate two or more arrays, one have to do is to list all elements of each arrays, and then build a new array. This sounds like create a List<T> and then calls toArray on it. Some other answers uses ArrayList, and that's fine. But how about implement our own? It is not hard:

    private static <T> T[] addAll(final T[] f, final T...o){
        return new AbstractList<T>(){
    
            @Override
            public T get(int i) {
                return i>=f.length ? o[i - f.length] : f[i];
            }
    
            @Override
            public int size() {
                return f.length + o.length;
            }
    
        }.toArray(f);
    }
    

    I believe the above is equivalent to solutions that uses System.arraycopy. However I think this one has its own beauty.

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