How can I concatenate two arrays in Java?

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

    Wow! lot of complex answers here including some simple ones that depend on external dependencies. how about doing it like this:

    String [] arg1 = new String{"a","b","c"};
    String [] arg2 = new String{"x","y","z"};
    
    ArrayList temp = new ArrayList();
    temp.addAll(Arrays.asList(arg1));
    temp.addAll(Arrays.asList(arg2));
    String [] concatedArgs = temp.toArray(new String[arg1.length+arg2.length]);
    

提交回复
热议问题