Java 实现向数组指定位置写入元素
来源:互联网 发布:多系统数据共享 编辑:程序博客网 时间:2024/05/22 04:24
思路
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。
0 0
- Java 实现向数组指定位置写入元素
- 向数组指定位置插入元素
- Java 实现从数组删除指定位置元素
- 输出数组指定位置元素
- 实现在一个数组指定位置添加元素和删除元素的功能
- Java实现字节数组中截取指定长度数组元素
- 在JS数组指定位置插入元素
- 在JS数组指定位置插入元素
- 在JS数组指定位置插入元素
- 添加元素到数组中的指定位置
- php删除指定位置数组元素
- java实现向有序数组中插入一个元素
- 数据结构(一)-顺序存储结构的插入和删除:向指定位置的数组插入和删除元素
- Java ArrayList add(index,element) 方法插入元素到数组指定位置
- Java ArrayList add(index,element) 方法插入元素到数组指定位置
- java数组中根据元素查找位置
- java删除数组中的任意位置元素
- java向数组中插入元素
- HDFS 架构解析
- C++中placement new操作符
- Tomcat 配置详解/优化方案
- IOS学习之——查看UDID 打包ipa 安装到客户手机上
- 利用指针P调用input函数的时候 ,不能使用student::input来调用input函数。
- Java 实现向数组指定位置写入元素
- 分类-1-逻辑回归(Logistic regression)、感知学习算法(perceptron learning algorithm)、牛顿迭代法
- nyoj开心的小明
- POJ 1251Jungle Roads
- 建立神经网络:Part 0
- 从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
- uva11552
- C是如何支持OC运行的
- linux命令echo和cat比较