Java tutorial
//package com.java2s; /* * ProGuard -- shrinking, optimization, obfuscation, and preverification * of Java bytecode. * * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import java.lang.reflect.Array; import java.util.Arrays; public class Main { /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static boolean[] ensureArraySize(boolean[] array, int size, boolean initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = new boolean[size]; if (initialValue) { Arrays.fill(array, 0, size, initialValue); } } return array; } /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static byte[] ensureArraySize(byte[] array, int size, byte initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = new byte[size]; if (initialValue != 0) { Arrays.fill(array, 0, size, initialValue); } } return array; } /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static short[] ensureArraySize(short[] array, int size, short initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = new short[size]; if (initialValue != 0) { Arrays.fill(array, 0, size, initialValue); } } return array; } /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static int[] ensureArraySize(int[] array, int size, int initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = new int[size]; if (initialValue != 0) { Arrays.fill(array, 0, size, initialValue); } } return array; } /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static long[] ensureArraySize(long[] array, int size, long initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = new long[size]; if (initialValue != 0L) { Arrays.fill(array, 0, size, initialValue); } } return array; } /** * Ensures the given array has a given size. * @param array the array. * @param size the target size of the array. * @param initialValue the initial value of the elements. * @return the original array, or a copy if it had to be * extended. */ public static Object[] ensureArraySize(Object[] array, int size, Object initialValue) { // Is the existing array large enough? if (array.length >= size) { // Reinitialize the existing array. Arrays.fill(array, 0, size, initialValue); } else { // Otherwise create and initialize a new array. array = (Object[]) Array.newInstance(array.getClass().getComponentType(), size); if (initialValue != null) { Arrays.fill(array, 0, size, initialValue); } } return array; } }