Here you can find the source of prepend(T element, T[] array)
Parameter | Description |
---|---|
element | The element to add to the beginning of the array. |
array | The array to add the element to. |
public static <T> T[] prepend(T element, T[] array)
//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; } }