zoukankan      html  css  js  c++  java
  • java 连接数组

    一,使用Apache Commons的ArrayUtils 

      Apache Commons类库有很多,几乎大多数的开源框架都依赖于它,Commons中的工具会节省你大部分时间,它包含一些常用的静态方法和Java的扩展。是开发中提高效率的一套框架.

        // apache-commons 的方法 ArrayUtils.addAll()
        String[] array1 = { "1", "2", "3" };
        String[] array2 = { "a", "b", "c" };
        String[] addAll = ArrayUtils.addAll(array1, array2); // 推荐
        System.out.println(Arrays.toString(addAll)); // [1, 2, 3, a, b, c]

    二,System.arraycopy()方法

      System.arraycopy(源数组,源数组起始位置,目标数组,目标数组起始位置,需要赋值数组元素长度);

        // System.arraycopy() 从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
        String[] c = new String[array1.length + array2.length];
        System.arraycopy(array1, 0, c, 0, array1.length);
        System.arraycopy(array2, 0, c, array1.length, array2.length);
        System.out.println(Arrays.toString(c)); // [1, 2, 3, a, b, c]

    三,Arrays.copyOf()方法

      Arrays.copyOf(要复制的数组,要返回副本数组的长度);

       // Arrays.copyOf() 复制指定的数组,截取或用 null 填充(如有必要),以使副本具有指定的长度。
        String[] copyOf = Arrays.copyOf(array1, array1.length + array2.length);
        System.arraycopy(array2, 0, copyOf, array1.length, array2.length);
        System.out.println(Arrays.toString(copyOf)); // [1, 2, 3, a, b, c]

    四,使用Arrays.copyOf()拼接多个数组

    public static void main(String[] args) {
    
            String[] array1 = { "1", "2", "3" };
            String[] array2 = { "a", "b", "c" };
            String[] array3 = { "一", "二", "三" };
            String[] concatAll = concatAll(array1, array2, array3);
            System.out.println(Arrays.toString(concatAll)); // [1, 2, 3, a, b, c, 一, 二, 三]
    
        }
    
        /**
         * 合并多个
         * 
         * @param first
         * @param rest
         * @return
         */
        public static <T> T[] concatAll(T[] first, T[]... rest) {
            int totalLength = first.length;
            for (T[] array : rest) {
                totalLength += array.length;
            }
            T[] result = Arrays.copyOf(first, totalLength);
            int offset = first.length;
            for (T[] array : rest) {
                System.arraycopy(array, 0, result, offset, array.length);
                offset += array.length;
            }
            return result;
        }
    T[]... rest  是java的不定长参数 
    java不定长参数参考:http://www.cnblogs.com/ooo0/p/7419777.html

    本文参考博客:http://blog.csdn.net/gplihf/article/details/52239756

  • 相关阅读:
    触发器
    累加求和存储过程
    check约束条件
    数据库的备份还原
    创建万能分页
    视图
    进销存存储过程
    函数存储过程
    数据库作业27~45
    数据库作业17~26
  • 原文地址:https://www.cnblogs.com/ooo0/p/7420071.html
Copyright © 2011-2022 走看看