Java 实现向数组指定位置写入元素
2016-04-02 17:20
549 查看
思路
1. 因为数组长度在初始化的时候是指定的并且不可变的,所以不能在原有的数组上直接进行添加操作,需要新建一个长度为当前长度加1的数组
2. 向新数组写数据
对比:
从时间复杂度来说insertElementByCopy的性能能优于insertElementByLoop,因为insertElementByLoop是0(n)而insertElementByCopy是0(1)。
从空间复杂度来说insertElementByCopy的性能能优于insertElementByLoop,因为insertElementByCopy需要更多次的swap。
下面是测试结果
1. 当原数组长度较少的时候.
took:0 ms by copy solution
took:2 ms by loop solution
2. 当原数组长度较大的时候
took:8 ms by copy solution
took:6 ms by loop solution
从测试结果可以看出来,在执行时间上两种处理方式都差不多。因为他们都需要维护新插入数据到指定的position。
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"; insertElem 8ee0 entByCopy(array, position, value); insertElementByLoop(array, position, value);---->
took:8 ms by copy solution
took:6 ms by loop solution
从测试结果可以看出来,在执行时间上两种处理方式都差不多。因为他们都需要维护新插入数据到指定的position。
相关文章推荐
- Think in Java(二):初始化与清理
- Step by Step into Spring(IOC)
- Spring MVC 常用的那些注解
- Java学习路线图
- 配置私服
- Java异常处理之throws抛出异常
- [JAVA]有关比较器(Comparator接口)的用法及对象数组排序问题
- 【JUC】JDK1.8源码分析之LockSupport(一)
- Spring事务异常回滚,捕获异常不抛出就不会回滚
- java中int和Integer的理解
- [JAVA]有关Java中Arrays.sort()的用法
- [core java学习笔记][第十一章异常断言日志调试]
- [core java学习笔记][第十一章异常断言日志调试]
- [core java学习笔记][第十章部署应用程序]
- [core java学习笔记][第十章部署应用程序]
- [core java学习笔记][第六章接口与内部类]
- [core java学习笔记][第五章继承]
- [core java学习笔记][第五章继承]
- [core java学习笔记][第六章接口与内部类]
- [core java学习笔记][第四章对象与类]