How can I concatenate two arrays in Java?

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

    A simple variation allowing the joining of more than one array:

    public static String[] join(String[]...arrays) {
    
        final List output = new ArrayList();
    
        for(String[] array : arrays) {
            output.addAll(Arrays.asList(array));
        }
    
        return output.toArray(new String[output.size()]);
    }
    

提交回复
热议问题