Java Array Copy copyAndFillOf(T[] original, int newLength, T padding)

Here you can find the source of copyAndFillOf(T[] original, int newLength, T padding)

Description

copy And Fill Of

License

Apache License

Declaration

static public <T> T[] copyAndFillOf(T[] original, int newLength,
            T padding) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

public class Main {
    static public <T> T[] copyAndFillOf(T[] original, int newLength,
            T padding) {/*from w  w  w.  j a  v  a2  s .co  m*/
        if (newLength < 0)
            throw new NegativeArraySizeException(
                    "The array size is negative.");
        T[] newArray = Arrays.copyOf(original, newLength);
        if (original.length < newLength) {
            System.arraycopy(original, 0, newArray, 0, original.length);
            Arrays.fill(newArray, original.length, newArray.length, padding);
        } else
            System.arraycopy(original, 0, newArray, 0, newLength);
        return newArray;

    }

    static public double[] copyAndFillOf(double[] original, int newLength,
            double padding) {
        if (newLength < 0)
            throw new NegativeArraySizeException(
                    "The array size is negative.");
        double[] newArray = new double[newLength];
        if (original.length < newLength) {
            System.arraycopy(original, 0, newArray, 0, original.length);
            Arrays.fill(newArray, original.length, newArray.length, padding);
        } else
            System.arraycopy(original, 0, newArray, 0, newLength);
        return newArray;
    }

    static public long[] copyAndFillOf(long[] original, int newLength,
            long padding) {
        if (newLength < 0)
            throw new NegativeArraySizeException(
                    "The array size is negative.");
        long[] newArray = new long[newLength];
        if (original.length < newLength) {
            System.arraycopy(original, 0, newArray, 0, original.length);
            Arrays.fill(newArray, original.length, newArray.length, padding);
        } else
            System.arraycopy(original, 0, newArray, 0, newLength);
        return newArray;
    }
}

Related

  1. copy(long[] v)
  2. copy(T[] array)
  3. copy(T[] array)
  4. copy2DArray(char[][] original)
  5. copy5DArray(char[][][][][] original)
  6. copyArray()
  7. copyArray(byte[] dest, int off, byte[] src)
  8. copyArray(byte[] original, int start, int length)
  9. copyArray(byte[] out, byte[] in, int idx)