How can I concatenate two arrays in Java?

后端 未结 30 2022
走了就别回头了
走了就别回头了 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:30

    A generic static version that uses the high performing System.arraycopy without requiring a @SuppressWarnings annotation:

    public static  T[] arrayConcat(T[] a, T[] b) {
        T[] both = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b, 0, both, a.length, b.length);
        return both;
    }
    

提交回复
热议问题