Java Array Range Copy copyOf(boolean[] array, int length)

Here you can find the source of copyOf(boolean[] array, int length)

Description

Returns a copy of the given array of size 1 greater than the argument.

License

Apache License

Parameter

Parameter Description
array The array to copy, must not be <code>null</code>.
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.

Declaration













private static boolean[] copyOf(boolean[] array, int length) 

Method Source Code

//package com.java2s;
/*//from  w  w  w . j  a va2 s .  com
 * 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 {
    /**
     * 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. copyOf(byte[] arr, int newLength)
  2. copyOf(byte[] b)
  3. copyOf(byte[] b, int off, int len)
  4. copyOf(byte[] b, int off, int len)