private static Object add(Object array, int index, Object element,
Class clss) {
if (array == null)
if (index != 0) {
throw new IndexOutOfBoundsException((new StringBuilder(
"Index: ")).append(index).append(", Length: 0")
.toString());
} else {
Object joinedArray = Array.newInstance(clss, 1);
Array.set(joinedArray, 0, element);
return joinedArray;
}
int length = Array.getLength(array);
if (index > length || index < 0)
throw new IndexOutOfBoundsException((new StringBuilder("Index: "))
.append(index).append(", Length: ").append(length)
.toString());
Object result = Array.newInstance(clss, length + 1);
System.arraycopy(array, 0, result, 0, index);//从array的0位置开始拷贝到result的0到index位置
Array.set(result, index, element);
if (index < length)
System.arraycopy(array, index, result, index + 1, length - index);//从array的index位置开始拷贝到result的index+1到length-index位置
return result;
}
Class clss) {
if (array == null)
if (index != 0) {
throw new IndexOutOfBoundsException((new StringBuilder(
"Index: ")).append(index).append(", Length: 0")
.toString());
} else {
Object joinedArray = Array.newInstance(clss, 1);
Array.set(joinedArray, 0, element);
return joinedArray;
}
int length = Array.getLength(array);
if (index > length || index < 0)
throw new IndexOutOfBoundsException((new StringBuilder("Index: "))
.append(index).append(", Length: ").append(length)
.toString());
Object result = Array.newInstance(clss, length + 1);
System.arraycopy(array, 0, result, 0, index);//从array的0位置开始拷贝到result的0到index位置
Array.set(result, index, element);
if (index < length)
System.arraycopy(array, index, result, index + 1, length - index);//从array的index位置开始拷贝到result的index+1到length-index位置
return result;
}