集册 Java实例教程 它接受一个数组并移除指定索引处的元素

它接受一个数组并移除指定索引处的元素

欢马劈雪     最近更新时间:2020-01-02 10:19:05

365
它需要一个Array并删除指定索引处的元素
/*来自 N o w  J a v a  .   c o m*/


//package com.nowjava;

import java.lang.reflect.Array;


public class Main {

    /**

     * It takes an Array and removes an element at the specified index

     *

     * @param <T> Type

     * @param typeClass the class of the type of the array

     * @param array the array you want to operate on

     * @param index the index of the element you want to remove

     * @return the new reduced array

     */

    public static <T> T[] removeElementAtIndex(Class<T> typeClass,

            T[] array, int index) {

        @SuppressWarnings("unchecked")

        T[] newArray = (T[]) Array.newInstance(typeClass, array.length - 1);

        int newArrayIndex = 0;

        for (int originalArrayIndex = 0; originalArrayIndex < array.length; originalArrayIndex++) {

            if (originalArrayIndex != index) 
展开阅读全文