How can I concatenate two arrays in Java?

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

    Or with the beloved Guava:

    String[] both = ObjectArrays.concat(first, second, String.class);
    

    Also, there are versions for primitive arrays:

    • Booleans.concat(first, second)
    • Bytes.concat(first, second)
    • Chars.concat(first, second)
    • Doubles.concat(first, second)
    • Shorts.concat(first, second)
    • Ints.concat(first, second)
    • Longs.concat(first, second)
    • Floats.concat(first, second)
    0 讨论(0)
  • 2020-11-21 06:40

    Please forgive me for adding yet another version to this already long list. I looked at every answer and decided that I really wanted a version with just one parameter in the signature. I also added some argument checking to benefit from early failure with sensible info in case of unexpected input.

    @SuppressWarnings("unchecked")
    public static <T> T[] concat(T[]... inputArrays) {
      if(inputArrays.length < 2) {
        throw new IllegalArgumentException("inputArrays must contain at least 2 arrays");
      }
    
      for(int i = 0; i < inputArrays.length; i++) {
        if(inputArrays[i] == null) {
          throw new IllegalArgumentException("inputArrays[" + i + "] is null");
        }
      }
    
      int totalLength = 0;
    
      for(T[] array : inputArrays) {
        totalLength += array.length;
      }
    
      T[] result = (T[]) Array.newInstance(inputArrays[0].getClass().getComponentType(), totalLength);
    
      int offset = 0;
    
      for(T[] array : inputArrays) {
        System.arraycopy(array, 0, result, offset, array.length);
    
        offset += array.length;
      }
    
      return result;
    }
    
    0 讨论(0)
  • 2020-11-21 06:40

    Using Java 8+ streams you can write the following function:

    private static String[] concatArrays(final String[]... arrays) {
        return Arrays.stream(arrays)
             .flatMap(Arrays::stream)
             .toArray(String[]::new);
    }
    
    0 讨论(0)
  • 2020-11-21 06:41

    I found a one-line solution from the good old Apache Commons Lang library.
    ArrayUtils.addAll(T[], T...)

    Code:

    String[] both = ArrayUtils.addAll(first, second);
    
    0 讨论(0)
  • 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<String> temp = new ArrayList<String>();
    temp.addAll(Arrays.asList(arg1));
    temp.addAll(Arrays.asList(arg2));
    String [] concatedArgs = temp.toArray(new String[arg1.length+arg2.length]);
    
    0 讨论(0)
  • 2020-11-21 06:42

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

    public static String[] join(String[]...arrays) {
    
        final List<String> output = new ArrayList<String>();
    
        for(String[] array : arrays) {
            output.addAll(Arrays.asList(array));
        }
    
        return output.toArray(new String[output.size()]);
    }
    
    0 讨论(0)
提交回复
热议问题