您的位置:首页 > 其它

数组复制 向数组添加字符串

2016-06-01 10:52 411 查看
     System.arraycopy()参数使用

     * @param      src      the source array.

     * @param      srcPos   starting position in the source array.

     * @param      dest     the destination array.

     * @param      destPos  starting position in the destination data.

     * @param      length   the number of array elements to be copied.


/**

** 两个数组合并


**/

public static String[] concatenateStringArrays(String[] array1, String[] array2) {

        if (ObjectUtils.isEmpty(array1)) {

            return array2;

        }

        if (ObjectUtils.isEmpty(array2)) {

            return array1;

        }

        String[] newArr = new String[array1.length + array2.length];

        System.arraycopy(array1, 0, newArr, 0, array1.length);

        System.arraycopy(array2, 0, newArr, array1.length, array2.length);

        return newArr;

    }


/**   向数组中添加元素

     * Append the given {@code String} to the given {@code String} array,

     * returning a new array consisting of the input array contents plus

     * the given {@code String}.

     * @param array the array to append to (can be {@code null})

     * @param str the {@code String} to append

     * @return the new array (never {@code null})

     */

    public static String[] addStringToArray(String[] array, String str) {

        if (ObjectUtils.isEmpty(array)) {

            return new String[] {str};

        }

        String[] newArr = new String[array.length + 1];

        System.arraycopy(array, 0, newArr, 0, array.length);

        newArr[array.length] = str;

        return newArr;

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  String 数组复制