• Java中如何把两个数组合并为一个


    http://freewind.me/blog/20110922/350.html

    Java中,如何把两个String[]合并为一个?

    看起来是一个很简单的问题。但是如何才能把代码写得高效简洁,却还是值得思考的。这里介绍四种方法,请参考选用。

    一、apache-commons

    这是最简单的办法。在apache-commons中,有一个ArrayUtils.addAll(Object[], Object[])方法,可以让我们一行搞定:

    String[] both = (String[]) ArrayUtils.addAll(first, second);

    其它的都需要自己调用jdk中提供的方法,包装一下。

    为了方便,我将定义一个工具方法concat,可以把两个数组合并在一起:

    static String[] concat(String[] first, String[] second) {}

    为了通用,在可能的情况下,我将使用泛型来定义,这样不仅String[]可以使用,其它类型的数组也可以使用:

    static <T> T[] concat(T[] first, T[] second) {}

    当然如果你的jdk不支持泛型,或者用不上,你可以手动把T换成String

    二、System.arraycopy()

    1. static String[] concat(String[] a, String[] b) {  
    2.    String[] c= new String[a.length+b.length];  
    3.    System.arraycopy(a, 0, c, 0, a.length);  
    4.    System.arraycopy(b, 0, c, a.length, b.length);  
    5.    return c;  
    6. }  

    使用如下:

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

    三、Arrays.copyOf()

    在java6中,有一个方法Arrays.copyOf(),是一个泛型函数。我们可以利用它,写出更通用的合并方法:

    1. public static <T> T[] concat(T[] first, T[] second) {  
    2.   T[] result = Arrays.copyOf(first, first.length + second.length);  
    3.   System.arraycopy(second, 0, result, first.length, second.length);  
    4.   return result;  
    5. }           

    如果要合并多个,可以这样写:

    1. public static <T> T[] concatAll(T[] first, T[]... rest) {  
    2.   int totalLength = first.length;  
    3.   for (T[] array : rest) {  
    4.     totalLength += array.length;  
    5.   }  
    6.   T[] result = Arrays.copyOf(first, totalLength);  
    7.   int offset = first.length;  
    8.   for (T[] array : rest) {  
    9.     System.arraycopy(array, 0, result, offset, array.length);  
    10.     offset += array.length;  
    11.   }  
    12.   return result;  
    13. }  

    使用如下:

    String[] both = concat(first, second);
    String[] more = concat(first, second, third, fourth);

    四、Array.newInstance

    还可以使用Array.newInstance来生成数组:

    1. private static <T> T[] concat(T[] a, T[] b) {  
    2.     final int alen = a.length;  
    3.     final int blen = b.length;  
    4.     if (alen == 0) {  
    5.         return b;  
    6.     }  
    7.     if (blen == 0) {  
    8.         return a;  
    9.     }  
    10.     final T[] result = (T[]) java.lang.reflect.Array.  
    11.             newInstance(a.getClass().getComponentType(), alen + blen);  
    12.     System.arraycopy(a, 0, result, 0, alen);  
    13.     System.arraycopy(b, 0, result, alen, blen);  
    14.     return result;  

  • 相关阅读:
    Markdown随手记
    主成分分析法(离散K-L变换)
    JAVAV EMAIL
    IDEA使用mybatis-generator
    java画海报二维码
    官网网址 学习指南
    Socket的用法——NIO包下SocketChannel的用法 ———————————————— 版权声明:本文为CSDN博主「茶_小哥」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/ycgslh/article/details/79604074
    Java NIO ———— Buffer 缓冲区详解 入门
    JVM类加载机制详解(二)类加载器与双亲委派模型
    mysql索引数据结构
  • 原文地址:https://www.cnblogs.com/baobeiqi-e/p/9884769.html
Copyright © 2020-2023  润新知