How can I concatenate two arrays in Java?

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

    Using the Java API:

    String[] f(String[] first, String[] second) {
        List both = new ArrayList(first.length + second.length);
        Collections.addAll(both, first);
        Collections.addAll(both, second);
        return both.toArray(new String[both.size()]);
    }
    

提交回复
热议问题