集册 Java实例教程 通过在末尾添加一个元素来展开数组。

通过在末尾添加一个元素来展开数组。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

502
通过在末尾添加一个元素来扩展数组。
/** 时 代 J a v a - N o w J a v a . c o m 提 供 **/


//package com.nowjava;


public class Main {

    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public short[] expandArray(short[] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public short[] expandArray(short[] src, int expandBy) {

        short[] dst;

        if (src == null) {
        /**
         * N o w  J a v a  . c o m 提 供 
        **/

            dst = new short[expandBy];

        } else {

            int n = src.length;

            dst = new short[n + expandBy];

            for (int j = 0; j < n; ++j) {

                dst[j] = src[j];

            }

        }

        return dst;

    }


    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public short[][] expandArray(short[][] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public short[][] expandArray(short[][] src, int expandBy) {

        short[][] dst;

        if (src == null) {

            dst = new short[expandBy][];

        } else {

            int n = src.length;

            dst = new short[n + expandBy][];

            for (int j = 0; j < n; ++j) {

                dst[j] = src[j];

            }

        }

        return dst;

    }


    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public int[] expandArray(int[] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public int[] expandArray(int[] src, int expandBy) {

        int[] dst;

        if (src == null) {

            dst = new int[expandBy];

        } else {

            int n = src.length;

            dst = new int[n + expandBy];

            for (int j = 0; j < n; ++j) {

                dst[j] = src[j];

            }

        }

        return dst;

    }


    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public long[] expandArray(long[] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public long[] expandArray(long[] src, int expandBy) {

        long[] dst;

        if (src == null) {

            dst = new long[expandBy];

        } else {

            int n = src.length;

            dst = new long[n + expandBy];

            for (int j = 0; j < n; ++j) {

                dst[j] = src[j];

            }

        }

        return dst;

    }


    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public float[] expandArray(float[] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public float[] expandArray(float[] src, int expandBy) {

        float[] dst;

        if (src == null) {

            dst = new float[expandBy];

        } else {

            int n = src.length;

            dst = new float[n + expandBy];

            for (int j = 0; j < n; ++j) {

                dst[j] = src[j];

            }

        }

        return dst;

    }


    /**

     * <p>Expand an array by adding one element to the end.</p>

     *

     * @param   src   an array

     * @return      an array that is one element longer

     */

    static public double[] expandArray(double[] src) {

        return expandArray(src, 1);

    }


    /**

     * <p>Expand an array by adding elements to the end.</p>

     *

     * @param   src      an array

     * @param   expandBy   the number of elements to add

     * @return         an array that is longer

     */

    static public double[] expandArray(double[] src, int expandBy) {

        double[] dst;

        if (src == null) {

            dst = new double[expandBy];

        } else {

            int n = src.length;

            dst = new double[n + e
展开阅读全文