Here you can find the source of remove(char[] array, int index)
Removes the element at the specified position from the specified array.
Parameter | Description |
---|---|
array | the array to remove the element from, may not be <code>null</code> |
index | the position of the element to be removed |
Parameter | Description |
---|---|
IndexOutOfBoundsException | if the index is out of range(index < 0 || index >= array.length), or if the array is <code>null</code>. |
public static char[] remove(char[] array, int index)
//package com.java2s; /*/* w ww . java 2s . co m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.Array; public class Main { /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove(["a"], 0) = [] * ArrayUtils.remove(["a", "b"], 0) = ["b"] * ArrayUtils.remove(["a", "b"], 1) = ["a"] * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ @SuppressWarnings("unchecked") // remove() always creates an array of the same type as its input public static <T> T[] remove(T[] array, int index) { return (T[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([true], 0) = [] * ArrayUtils.remove([true, false], 0) = [false] * ArrayUtils.remove([true, false], 1) = [true] * ArrayUtils.remove([true, true, false], 1) = [true, false] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static boolean[] remove(boolean[] array, int index) { return (boolean[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1], 0) = [] * ArrayUtils.remove([1, 0], 0) = [0] * ArrayUtils.remove([1, 0], 1) = [1] * ArrayUtils.remove([1, 0, 1], 1) = [1, 1] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static byte[] remove(byte[] array, int index) { return (byte[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove(['a'], 0) = [] * ArrayUtils.remove(['a', 'b'], 0) = ['b'] * ArrayUtils.remove(['a', 'b'], 1) = ['a'] * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c'] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static char[] remove(char[] array, int index) { return (char[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1.1], 0) = [] * ArrayUtils.remove([2.5, 6.0], 0) = [6.0] * ArrayUtils.remove([2.5, 6.0], 1) = [2.5] * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static double[] remove(double[] array, int index) { return (double[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1.1], 0) = [] * ArrayUtils.remove([2.5, 6.0], 0) = [6.0] * ArrayUtils.remove([2.5, 6.0], 1) = [2.5] * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static float[] remove(float[] array, int index) { return (float[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1], 0) = [] * ArrayUtils.remove([2, 6], 0) = [6] * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static int[] remove(int[] array, int index) { return (int[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1], 0) = [] * ArrayUtils.remove([2, 6], 0) = [6] * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static long[] remove(long[] array, int index) { return (long[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * <pre> * ArrayUtils.remove([1], 0) = [] * ArrayUtils.remove([2, 6], 0) = [6] * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] * </pre> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ public static short[] remove(short[] array, int index) { return (short[]) remove((Object) array, index); } /** * <p>Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).</p> * * <p>This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.</p> * * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException * will be thrown, because in that case no valid index can be specified.</p> * * @param array the array to remove the element from, may not be <code>null</code> * @param index the position of the element to be removed * @return A new array containing the existing elements except the element * at the specified position. * @throws IndexOutOfBoundsException if the index is out of range * (index < 0 || index >= array.length), or if the array is <code>null</code>. * @since 2.1 */ private static Object remove(Object array, int index) { int length = getLength(array); if (index < 0 || index >= length) { throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length); } Object result = Array.newInstance(array.getClass() .getComponentType(), length - 1); System.arraycopy(array, 0, result, 0, index); if (index < length - 1) { System.arraycopy(array, index + 1, result, index, length - index - 1); } return result; } /** * <p>Returns the length of the specified array. * This method can deal with <code>Object</code> arrays and with primitive arrays.</p> * * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p> * * <pre> * ArrayUtils.getLength(null) = 0 * ArrayUtils.getLength([]) = 0 * ArrayUtils.getLength([null]) = 1 * ArrayUtils.getLength([true, false]) = 2 * ArrayUtils.getLength([1, 2, 3]) = 3 * ArrayUtils.getLength(["a", "b", "c"]) = 3 * </pre> * * @param array the array to retrieve the length from, may be null * @return The length of the array, or <code>0</code> if the array is <code>null</code> * @throws IllegalArgumentException if the object arguement is not an array. * @since 2.1 */ public static int getLength(Object array) { if (array == null) { return 0; } return Array.getLength(array); } }