Java 实现向数组指定位置写入元素

思路

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。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值