Java Array Shift unshift(String[] array, String newElem)

Here you can find the source of unshift(String[] array, String newElem)

Description

prepends newElem to the array, shifting all elements one index up.

License

Open Source License

Declaration

public static String[] unshift(String[] array, String newElem) 

Method Source Code

//package com.java2s;
/*/*from   ww  w  .ja  va2 s  . c  o m*/
 * This file is part of ROOSSTER.
 * Copyright 2004, Benjamin Reitzammer <benjamin@roosster.org>
 * All rights reserved.
 *
 * ROOSSTER is free software; you can redistribute it and/or modify
 * it under the terms of the Artistic License.
 *
 * You should have received a copy of the Artistic License
 * along with ROOSSTER; if not, go to
 * http://www.opensource.org/licenses/artistic-license.php for details
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;

public class Main {
    /**
     * prepends <code>newElem</code> to the array, shifting all elements one index up.
     */
    public static String[] unshift(String[] array, String newElem) {
        List arr = new ArrayList(Arrays.asList(array));
        arr.add(0, newElem);
        return (String[]) arr.toArray(new String[0]);
    }
}

Related

  1. shiftRightN(int[] block, int n)
  2. shiftRightN(int[] block, int n)
  3. shiftRightN(int[] x, int n)
  4. shiftRightState(int[] state)
  5. shiftStringArray(String[] input)