java列表删除指定位置元素_怎么删除数组中的某个元素

java列表删除指定位置元素_怎么删除数组中的某个元素Java实现从数组删除指定位置元素,比较通过遍历和copyarray两种方式的性能差别

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

思路

1.  因为数组长度在初始化的时候是指定的并且不可变的,所以不能在原有的数组上直接进行删除操作,需要新建一个长度为当前长度减1的数组

2.  向新数组写数据

/**
     * remove element at the specified position from the given array by loop
     * 
     * @param array
     * @param position
     * @return
     */
    public static String[] removeElementByLoop(String[] array, int position) {
        if (position < 0 || position > array.length) {
            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 < array.length; i++) {
            if (i < index) {
                newArray[i] = array[i];
            } else if (i > index) {
                newArray[i - 1] = array[i];
            }
        }
        System.out.println("took:" + (System.currentTimeMillis() - startTime) + " ms by loop solution");
        return newArray;
    }

    /**
     * remove element at the specified position from the given array by copy
     * 
     * @param array
     * @param position
     * @return
     */
    public static String[] removeElementByCopy(String[] array, int position) {
        int length = array.length;
        if (position < 0 || position > length) {
            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;
        System.arraycopy(array, 0, newArray, 0, index);
        if (index < length - 1) {
            System.arraycopy(array, index + 1, newArray, index, length - index - 1);
        }
        System.out.println("took:" + (System.currentTimeMillis() - startTime) + " ms by copy solution");
        return newArray;
    }

对比:
从时间复杂度来说removeElementByCopy的性能能优于removeElementByLoop,因为removeElementByLoop是0(n)而removeElementByCopy是0(1)。

从空间复杂度来说removeElementByLoop的性能能优于removeElementByCopy,因为removeElementByCopy需要更多次的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 = 80000;
removeElementByCopy(array, position);
removeElementByLoop(array, position);

—->

took:0 ms by copy solution

took:2 ms by loop solution

2.

List<String> elements = new ArrayList<String>();
for (int i = 0; i < 1000000; i++) {
    elements.add(i + "");
}
String[] array = elements.toArray(new String[elements.size()]);
int position = 80000;
removeElementByCopy(array, position);
removeElementByLoop(array, position);

—->

took:7 ms by copy solution

took:88 ms by loop solution

从测试结果可以看出来,在执行时间上的花费,removeElementByCopy的效率明显高于removeElementByLoop

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/169495.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python闭包详解_python闭包的使用场景

    python闭包详解_python闭包的使用场景闭包首先了解一下:如果在一个函数的内部定义了另一个函数,外部的我们叫他外函数,内部的我们叫他内函数。在一个外函数中定义了一个内函数,内函数里运用了外函数的临时变量,并且外函数的返回值是内函数的引用

    2022年7月29日
    8
  • iOS 处理pfx文件

    iOS 处理pfx文件先普及下基础知识,pfx是公钥加密技术12号标准(PublicKeyCryptographyStandards#12,PKCS#12)为存储和传输用户或服务器私钥、公钥和证书而指定的一个可移植的格式。它是一种二进制格式,这些文件也称为PFX文件。详见百科我的pfx文件是由写C++的同事提供,里面包含秘钥,使用的时候需要先读取里面的数据,然后对数据进行base64编码,最后获得字

    2022年6月1日
    43
  • Hadoop mapreduce过程key 和value分别存什么值

    Hadoop mapreduce过程key 和value分别存什么值Hadoop mapreduce过程key 和value分别存什么值

    2022年4月23日
    54
  • java课程设计学生信息管理系统。

    java课程设计学生信息管理系统。importjava.awt.FlowLayout;importjavax.swing.JFrame;importjavax.swing.JPanel;//主函数publicclasstest01{publicstaticvoidmain(String[]args){MyFrame01my=newMyFrame01();…

    2022年7月12日
    21
  • 一台计算机如何安装2个版本的python,互不影响呢[通俗易懂]

    一台计算机如何安装2个版本的python,互不影响呢[通俗易懂]python学习过程中,很多教程都是python2版本的但是python2到2020年就不在维护了,所以现在教大家如何在一台计算机上安装python2和python3互不影响,可以自如的切换。不用任何第三方软件简单省力,一次配置就可。第一步下载适合自己电脑的安装包32还是64按照自己的电脑来:python2.7.13和python3.6先安装哪一个版本无所谓。不…

    2022年5月8日
    180
  • 热拔插更换硬盘方法

    热拔插更换硬盘方法热拔插更换硬盘方法如果要替换的磁盘已做了镜像,推荐如下步骤:1.删除该磁盘上所有逻辑卷的复件,使用rmlvcopy命令或unmirrorvg命令。2.从卷组中删除该磁盘,使用reducevg命令。3.使用rmdev命令删除该磁盘定义。4.物理移除该磁盘。如果磁盘不是可热交换的(hot-swappable),可能要求重启系统。5.使备用的磁盘可用。如果磁盘是可

    2022年5月30日
    37

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号