Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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;

public class Main {
    /**
     * Inserts the given element in the given array.
     * The array is extended if necessary.
     * @param array   the array.
     * @param size    the original size of the array.
     * @param index   the index at which the element is to be added.
     * @param element the element to be added.
     * @return        the original array, or a copy if it had to be extended.
     */
    public static byte[] insert(byte[] array, int size, int index, byte element) {
        array = extendArray(array, size + 1);

        // Move the last part.
        System.arraycopy(array, index, array, index + 1, size - index);

        array[index] = element;

        return array;
    }

    /**
     * Inserts the given element in the given array.
     * The array is extended if necessary.
     * @param array   the array.
     * @param size    the original size of the array.
     * @param index   the index at which the element is to be added.
     * @param element the element to be added.
     * @return        the original array, or a copy if it had to be extended.
     */
    public static short[] insert(short[] array, int size, int index, short element) {
        array = extendArray(array, size + 1);

        // Move the last part.
        System.arraycopy(array, index, array, index + 1, size - index);

        array[index] = element;

        return array;
    }

    /**
     * Inserts the given element in the given array.
     * The array is extended if necessary.
     * @param array   the array.
     * @param size    the original size of the array.
     * @param index   the index at which the element is to be added.
     * @param element the element to be added.
     * @return        the original array, or a copy if it had to be extended.
     */
    public static int[] insert(int[] array, int size, int index, int element) {
        array = extendArray(array, size + 1);

        // Move the last part.
        System.arraycopy(array, index, array, index + 1, size - index);

        array[index] = element;

        return array;
    }

    /**
     * Inserts the given element in the given array.
     * The array is extended if necessary.
     * @param array   the array.
     * @param size    the original size of the array.
     * @param index   the index at which the element is to be added.
     * @param element the element to be added.
     * @return        the original array, or a copy if it had to be extended.
     */
    public static long[] insert(long[] array, int size, int index, long element) {
        array = extendArray(array, size + 1);

        // Move the last part.
        System.arraycopy(array, index, array, index + 1, size - index);

        array[index] = element;

        return array;
    }

    /**
     * Inserts the given element in the given array.
     * The array is extended if necessary.
     * @param array   the array.
     * @param size    the original size of the array.
     * @param index   the index at which the element is to be added.
     * @param element the element to be added.
     * @return        the original array, or a copy if it had to be extended.
     */
    public static Object[] insert(Object[] array, int size, int index, Object element) {
        array = extendArray(array, size + 1);

        // Move the last part.
        System.arraycopy(array, index, array, index + 1, size - index);

        array[index] = element;

        return array;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static boolean[] extendArray(boolean[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        boolean[] newArray = new boolean[size];

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static byte[] extendArray(byte[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        byte[] newArray = new byte[size];

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static short[] extendArray(short[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        short[] newArray = new short[size];

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static int[] extendArray(int[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        int[] newArray = new int[size];

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static long[] extendArray(long[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        long[] newArray = new long[size];

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }

    /**
     * Ensures the given array has a given size.
     * @param array the array.
     * @param size  the target size of the array.
     * @return      the original array, or a copy if it had to be extended.
     */
    public static Object[] extendArray(Object[] array, int size) {
        // Reuse the existing array if possible.
        if (array.length >= size) {
            return array;
        }

        // Otherwise create and initialize a new array.
        Object[] newArray = (Object[]) Array.newInstance(array.getClass().getComponentType(), size);

        System.arraycopy(array, 0, newArray, 0, array.length);

        return newArray;
    }
}