非淡泊无以明志,非宁静无以致远。非学无以广才,非志无以成学。

Java合并两个数组或多个数组

/**
     * 将多个数组合并成一个新的数组
     * @param arrays
     * @return
     */
    public static Object[] arrayCopyMerge(Object[]... arrays){
        //数组长度
        int arrayLength = 0;
        //目标数组的起始位置
        int startIndex = 0;

        for(Object[] file : arrays){
            arrayLength = arrayLength + file.length;
        }

        Object[] fileArray = new Object[arrayLength];

        for(int i = 0; i < arrays.length; i++){

            if(i > 0){
                //i为0 时,目标数组的起始位置为0 ,i为1时,目标数组的起始位置为第一个数组长度
                //i为2时,目标数组的起始位置为第一个数组长度+第二个数组长度
                startIndex = startIndex + arrays[i-1].length;
            }
       //复制一个新的数组
            System.arraycopy(arrays[i], 0, fileArray, startIndex, arrays[i].length);

        }
        return fileArray;
    }

 

posted @ 2021-05-27 18:45  Happy丶小鱼  阅读(1063)  评论(0编辑  收藏  举报