Java Array Resize prepend(T element, T[] array)

Here you can find the source of prepend(T element, T[] array)

Description

Adds the element to the beginning of the array and returns the new array.

License

Open Source License

Parameter

Parameter Description
element The element to add to the beginning of the array.
array The array to add the element to.

Return

The reference to the new array starting with the element.

Declaration

public static <T> T[] prepend(T element, T[] array) 

Method Source Code


//package com.java2s;
/* /*from   w  w w  . j  av a2 s  .c  o m*/
 * NOTICE OF LICENSE
 * 
 * This source file is subject to the Open Software License (OSL 3.0) that is 
 * bundled with this package in the file LICENSE.txt. It is also available 
 * through the world-wide-web at http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to obtain it 
 * through the world-wide-web, please send an email to magnos.software@gmail.com 
 * so we can send you a copy immediately. If you use any of this software please
 * notify me via our website or email, your feedback is much appreciated. 
 * 
 * @copyright   Copyright (c) 2011 Magnos Software (http://www.magnos.org)
 * @license     http://opensource.org/licenses/osl-3.0.php
 *             Open Software License (OSL 3.0)
 */

import java.util.Arrays;

public class Main {
    /**
     * Adds the element to the beginning of the array and returns the new array.
     * 
     * @param element
     *        The element to add to the beginning of the array.
     * @param array
     *        The array to add the element to.
     * @return The reference to the new array starting with the element.
     */
    public static <T> T[] prepend(T element, T[] array) {
        array = Arrays.copyOf(array, array.length + 1);
        System.arraycopy(array, 0, array, 1, array.length - 1);
        array[0] = element;
        return array;
    }
}

Related

  1. arrayResize(double[] source, int targetSize)
  2. prepend(String firstElement, String[] remaining)
  3. prependArg(String args[], String newArg)
  4. prependZeros(int n, byte[] message)
  5. resize2(double[][] array, int newsize1, int newsize2, double padding)
  6. resizeArray(final T[] array, final int newSize)