/**
* 复制除给定下标之外的数组元素
* @param array 一个String数组
* @param index 在长度之内的下标
* @return 一个新数组,若原数组长度不大于1,返回null
* @throws ArrayIndexOutOfBoundsException 下标值应该在数组范围内
*/
public String[] copyExcept(String[] array, int index) throws ArrayIndexOutOfBoundsException {
// 初始新数组为null
String[] newArray = null;
// 复制的原数组长度必须大于1,否则复制的数组将为null
if (array != null && array.length > 1) {
// index在数组下标之内,否则抛出异常
if (index >= 0 && index < array.length) {
// 新数组长度比原数组少一
newArray = new String[array.length - 1];
int newIndex = -1;// 初始新数组下标
for (int i = 0; i < array.length; i++) {
if (i == index) {
continue;// 当等于给定下标值时,不进行复制
} else {
newIndex += 1;
newArray[newIndex] = array[i];
}
}
} else {
throw new ArrayIndexOutOfBoundsException("下标值应该大于等于零且小于" + array.length);
}
}
return newArray;
}