思路
1. 因为数组长度在初始化的时候是指定的并且不可变的,所以不能在原有的数组上直接进行添加操作,需要新建一个长度为当前长度加1的数组
2. 向新数组写数据
/**
* insert the input element into the specified position by copy the input
* array
*
* @param array
* @param position
* @param value
* @return
*/
public static String[] insertElementByCopy(String[] array, int position, String value) {
int length = array.length;
if (position < 0 || position > length + 1) {
throw new IndexOutOfBoundsException("the position is out of the array indices");
}
long startTime = System.currentTimeMillis();
String[] newArray = new String[array.length + 1];
int index = position - 1;
if (index == 0) {
// copy 0~end elements to the new array 1~end
System.arraycopy(array, 0, newArray, 1, length);
} else {
// copy 0~index elements to the new array 0~index
System.arraycopy(array, 0, newArray, 0, index);
if (index < length)
// copy index~end elements to the new array index+1~end
System.arraycopy(array, index, newArray, index + 1, length - index);
}
// set the input value to the new array at specified index
newArray[index] = value;
System.out.println("took:" + (System.currentTimeMillis() - startTime) + " ms by copy solution");
return newArray;
}
/**
* insert the input element into the specified position by loop the input
* array
*
* @param array
* @param position
* @param value
* @return
*/
public static String[] insertElementByLoop(String[] array, int position, String value) {
if (position < 0 || position > array.length + 1) {
throw new IndexOutOfBoundsException("the position is out of the array indices");
}
long startTime = System.currentTimeMillis();
String[] newArray = new String[array.length + 1];
int index = position - 1;
for (int i = 0; i < newArray.length; i++) {
// it should insert input value when the input position -1 equals i
String newValue = value;
if (i < index) {
// it should insert the original value when the input position
// -1 is greater than i
newValue = array[i];
} else if (i > index) {
// it should insert the previous value when the input position
// -1 is less than i
newValue = array[i - 1];
}
newArray[i] = newValue;
}
System.out.println("took:" + (System.currentTimeMillis() - startTime) + " ms by loop solution");
return newArray;
}
对比:
从时间复杂度来说insertElementByCopy的性能能优于insertElementByLoop,因为insertElementByLoop是0(n)而insertElementByCopy是0(1)。
从空间复杂度来说insertElementByCopy的性能能优于insertElementByLoop,因为insertElementByCopy需要更多次的swap。
下面是测试结果
1. 当原数组长度较少的时候.
List<String> elements = new ArrayList<String>();
for (int i = 0; i <90000; i++) {
elements.add(i + "");
}
String[] array = elements.toArray(new String[elements.size()]);
int position = 5000;
String value = "T";
insertElementByCopy(array, position, value);
insertElementByLoop(array, position, value);
---->
took:0 ms by copy solution
took:2 ms by loop solution
2. 当原数组长度较大的时候
List<String> elements = new ArrayList<String>();
for (int i = 0; i <90000; i++) {
elements.add(i + "");
}
String[] array = elements.toArray(new String[elements.size()]);
int position = 5000;
String value = "T";
insertElementByCopy(array, position, value);
insertElementByLoop(array, position, value);
---->
took:8 ms by copy solution
took:6 ms by loop solution
从测试结果可以看出来,在执行时间上两种处理方式都差不多。因为他们都需要维护新插入数据到指定的position。