它需要一个Array并将一个元素插入到指定的索引
/* 来 自* 时代Java - 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 inserts an element to 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 where you want to insert the element * @param element the element you want to add * @return the new extended array */ public static <T> T[] insertElement(Class<T> typeClass, T[] array, int index, T element) { if (index > array.length - 1) { return addElement(typeClass, array, element); } @SuppressWarnings("unchecked") T[] newArray = (T[]) Array.newInstance(typeClass, array.length + 1); int newArrayIndex = 0; for (int originalArrayIndex = 0; originalArrayIndex < array.length; originalArrayIndex++) { if (newArrayIndex == index) { newArray[newArrayIndex] = element; newArrayIndex++; } /** from N o w J a v a . c o m - 时 代 Java**/ newArray[newArrayIndex] = array[originalArrayIndex]; newArrayIndex++; } return newArray; } /** * It takes an Array and add the specified element to the end of the it * * @param <T> Type * @param typeClass the class of the type of the array * @param array the array you want to operate on * @param element the element you want to add * @return the new extended array */ public static <T> T[] addElement(Class<T> typeClass, T[] array, T element) {