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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言基础:函数的定义与调用[通俗易懂]

    C语言基础:函数的定义与调用[通俗易懂]    在前面内容中我们调用了一个标准C的库函数,叫printf,那么如果我们想自己定义函数应该如何来编写程序呢?定义函数又有什么好处呢?因为我们在教材中提及到的例子主要目的是为了让读者对程序的原理有一定的了解,所以设定的例子程序通常都比较简单,基本上在几行到十几行,多一点的也就三五十行代而已,但是在真正的编程工作中,我们需要完成的代码将非常大,所以将代码合理的分为不同的区块是很有必要的,…

    2022年6月30日
    29
  • mac配置IDEA热部署[通俗易懂]

    mac配置IDEA热部署[通俗易懂]1、pom文件中添加依赖<!–开启热部署–><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional&gt

    2022年6月8日
    171
  • Math.abs( x )

    Math.abs( x )

    2021年10月18日
    64
  • Fragment 点击返回键 做返回操作

    Fragment 点击返回键 做返回操作

    2021年3月12日
    179
  • 单片机的io口的结构_单片机io口结构对比异同点

    单片机的io口的结构_单片机io口结构对比异同点文章目录单片机的最小系统(纯电路)单片机最小系统电路图STC89c52最小系统增强型8051最小系统晶振的样子STC单片机复位IO口结构写0亮灯写1灭灯强推挽模式写1输出5V大电流开漏模式写0输出0V不能拉高高阻输入单片机的最小系统(纯电路)单片机最小系统电路图1.电源供电:5V的正极接VCC脚,5V的负极接GND2.时钟电路:提供时钟信号,内存IRC震荡或者外部晶振参与震荡3.复位电路:避免上电过程中执行程序进而产生误动作甚至损坏电路STC89c52最小系统增强型8051最小系统

    2022年10月21日
    0
  • mysql workbench怎么改密码_mysql notifier

    mysql workbench怎么改密码_mysql notifier更改MySQL用户密码MySQL用户是一条记录,其中包含登录信息,帐户特权以及MySQL帐户访问和管理数据库的主机信息。登录信息包括用户名和密码。在某些情况下,需要更改MySQL数据库中的用户密码。要更改任何用户帐户的密码,必须记住以下信息:您要更改的用户帐户的详细信息。用户要更改其密码的应用程序。如果您在不更改应用程序连接字符串的情况下重置了用户帐户密码,则该应用程序将无法与数据库服务器连接。M…

    2022年8月12日
    38

发表回复

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

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