Java Array Clone clone(boolean[] array)

Here you can find the source of clone(boolean[] array)

Description

Clones an array returning a typecast result and handling null.

License

Apache License

Parameter

Parameter Description
array the array to clone, may be <code>null</code>

Return

the cloned array, null if null input

Declaration

public static boolean[] clone(boolean[] array) 

Method Source Code

//package com.java2s;
/*/*from   w ww.  j  av  a  2s  . c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static boolean[] clone(boolean[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static byte[] clone(byte[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static char[] clone(char[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static double[] clone(double[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static float[] clone(float[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static int[] clone(int[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static long[] clone(long[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Shallow clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * The objects in the array are not cloned, thus there is no special
     * handling for multi-dimensional arrays.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to shallow clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static Object[] clone(Object[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * <p>
     * Clones an array returning a typecast result and handling
     * <code>null</code>.
     * </p>
     *
     * <p>
     * This method returns <code>null</code> for a <code>null</code> input
     * array.
     * </p>
     *
     * @param array
     *            the array to clone, may be <code>null</code>
     * @return the cloned array, <code>null</code> if <code>null</code> input
     */
    public static short[] clone(short[] array) {
        if (array == null) {
            return null;
        }
        return copyOf(array, array.length);
    }

    /**
     * Returns a copy of the given array of size 1 greater than the argument.
     * The last value of the array is left to the default value.
     *
     * @param array
     *            The array to copy, must not be <code>null</code>.
     * @param newArrayComponentType
     *            If <code>array</code> is <code>null</code>, create a size 1
     *            array of this type.
     * @return A new copy of the array of size 1 greater than the input.
     */
    //   private static Object copyArrayGrow1(Object array,
    //         Class newArrayComponentType) {
    //      if (array != null) {
    //         int arrayLength = Array.getLength(array);
    //         Object newArray = Array.newInstance(array.getClass()
    //               .getComponentType(), arrayLength + 1);
    //         System.arraycopy(array, 0, newArray, 0, arrayLength);
    //         return newArray;
    //      }
    //      return Array.newInstance(newArrayComponentType, 1);
    //   }

    private static boolean[] copyOf(boolean[] array, int length) {
        boolean[] anew = new boolean[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = false;
        }
        return anew;
    }

    private static byte[] copyOf(byte[] array, int length) {
        byte[] anew = new byte[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0;
        }
        return anew;
    }

    private static char[] copyOf(char[] array, int length) {
        char[] anew = new char[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = '\u0000';
        }
        return anew;
    }

    private static double[] copyOf(double[] array, int length) {
        double[] anew = new double[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0d;
        }
        return anew;
    }

    private static float[] copyOf(float[] array, int length) {
        float[] anew = new float[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0f;
        }
        return anew;
    }

    private static int[] copyOf(int[] array, int length) {
        int[] anew = new int[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0;
        }
        return anew;
    }

    private static long[] copyOf(long[] array, int length) {
        long[] anew = new long[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0l;
        }
        return anew;
    }

    private static Object[] copyOf(Object[] array, int length) {
        Object[] anew = new Object[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = null;
        }
        return anew;
    }

    private static short[] copyOf(short[] array, int length) {
        short[] anew = new short[length];
        for (int i = 0; i < length; i++) {
            if (i < array.length) {
                anew[i] = array[i];
                continue;
            }
            anew[i] = 0;
        }
        return anew;
    }
}

Related

  1. clone(boolean[] array)
  2. clone(boolean[] in)
  3. clone(byte[] input)
  4. clone(byte[] orig)