Java Array Remove removeAt(T[] array, int index)

Here you can find the source of removeAt(T[] array, int index)

Description

Removes the element at the index from the array and returns a new array.

License

Open Source License

Parameter

Parameter Description
array The array to remove an element from.
index The index of the element to remove.

Return

The reference to a new array without the element at the index.

Declaration

public static <T> T[] removeAt(T[] array, int index) 

Method Source Code


//package com.java2s;
/* // ww w . j  a  v  a  2 s  . co  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 {
    /**
     * Removes the element at the index from the array and returns a new array.
     * 
     * @param array
     *        The array to remove an element from.
     * @param index
     *        The index of the element to remove.
     * @return The reference to a new array without the element at the index.
     */
    public static <T> T[] removeAt(T[] array, int index) {
        System.arraycopy(array, index + 1, array, index, array.length - index - 1);
        return Arrays.copyOf(array, array.length - 1);
    }
}

Related

  1. remove(T[] a, int i)
  2. removeAll(E[] array, Object... toRemove)
  3. removeAll(T[] array, Collection toRemove)
  4. removeAll(T[] items, T item)
  5. RemoveArgs(String[] args, int startIndex)
  6. removeByPrefix(String[] array, String prefix)
  7. removeByteOrderMark(final byte[] input)
  8. removeCommonWords(String[] words)
  9. removeElement(final E[] array, final int index)