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


相关推荐

  • 【转载】面试?或许你应该这样

    【转载】面试?或许你应该这样

    2021年11月20日
    50
  • ssis 数据转换_SSIS数据类型:高级编辑器的更改与数据转换的转换

    ssis 数据转换_SSIS数据类型:高级编辑器的更改与数据转换的转换ssis数据转换Inthisarticle,IwillfirstgiveanoverviewofSSISdatatypesanddatatypesconversionmethodsandthenIwillillustratethedifferencebetweenchangingthecolumnsdatatypesfrom…

    2022年7月20日
    16
  • vue源码实现的整体流程解析「建议收藏」

    vue源码实现的整体流程解析「建议收藏」一、前言最近一直在使用vue做项目,闲暇之余查阅了一些关于vue实现原理的资料,一方面对所了解到的知识做个总结,另外一方面希望能对看到此文章的同学有所帮助。本文如有不足之处,还请过往的大佬批评指正。

    2022年8月4日
    5
  • T-SQL聚合函数

    T-SQL聚合函数SQLServer2008联机丛书(2009年1月)聚合函数(Transact-SQL)聚合函数对一组值执行计算,并返回单个值。除了COUNT以外,聚合函数都会忽略空值。聚合函数经常与SELECT语句的GROUPBY子句一起使用。所有聚合函数均为确定性函数。这表示任何时候使用一组特定的输入值调用聚合函数,所返回的值都是相同的。有关函数确定性的

    2022年6月21日
    28
  • 分布式CAP原理

    分布式CAP原理Consistentdata:    Operationscommitorfailintheirentirety(atomic)    操作的提交或者失败都是整体的(原子性)    Transactionsneverobserveorresultininconsistentdata(consistent)        事务不会导致不一致的数据  

    2022年5月19日
    37
  • JavaScript 解构赋值

    JavaScript 解构赋值文章目录JavaScript解构数组解构分配默认值交换变量跳过项将剩余元素分配给单个变量嵌套解构赋值参考文档    在本教程中,您将借助示例了解JavaScript解构赋值。JavaScript解构    ES6中引入的解构赋值可以轻松地将数组值和对象属性分配给不同的变量。例如,    在ES6之前://assigningobjectattributestovariablesconstperson={name:’Sara’,age:25,

    2025年5月24日
    2

发表回复

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

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