Expand an array by adding one element to the end. - Java Collection Framework

Java examples for Collection Framework:Array Auto Increment

Description

Expand an array by adding one element to the end.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w w w  .  j a v  a2  s  . com*/
     * <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 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 + 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 String[] expandArray(String[] 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 String[] expandArray(String[] src, int expandBy) {
        String[] dst;
        if (src == null) {
            dst = new String[expandBy];
        } else {
            int n = src.length;
            dst = new String[n + expandBy];
            for (int j = 0; j < n; ++j) {
                dst[j] = src[j];
            }
        }
        return dst;
    }
}

Related Tutorials