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

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


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

相关推荐

  • 跟我一起数据挖掘(20)——网站日志挖掘

    跟我一起数据挖掘(20)——网站日志挖掘收集web日志的目的Web日志挖掘是指采用数据挖掘技术,对站点用户访问Web服务器过程中产生的日志数据进行分析处理,从而发现Web用户的访问模式和兴趣爱好等,这些信息对站点建设潜在有用的可理解的未知信息和知识,用于分析站点的被访问情况,辅助站点管理和决策支持等。1、以改进web站点设计为目标,通过挖掘用户聚类和用户的频繁访问路径,修改站点的页面之间的链接关系,以适应用户的访问习惯…

    2022年9月25日
  • python3.9多线程_python多线程没用

    python3.9多线程_python多线程没用什么是线程?线程也叫轻量级进程,是操作系统能够进行运算调度的最小单位,它被包涵在进程之中,是进程中的实际运作单位。线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其

    2022年7月29日
  • 授权使用微信登陆第三方_微信图标改成WeChat

    授权使用微信登陆第三方_微信图标改成WeChat第三方平台系列文章,今天终于又开始更新了,今天继续学习微信(wechat)授权第三方登录一、准备工作1、申请微信公众测试号由于我们是个人开发者,我们需要去注册申请一个微信公众平台的测试号https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login我们使用微信扫码登录后,我们可以拿到appID和appsecret2、关注公众测试号3、配置回调域名在“网页服务”中找到“网页账号”,修改“网页授权获取用户基本信息”接口的

    2022年10月22日
  • #从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题

    #从源头解决# 自定义头文件在VS上出现“无法打开源文件“XX.h“的问题自己编写了一个头文件,在主函数中通过#include引用时出现了无法打开源文件的问题,通过网上查阅,发现是自己混淆了#include<>和#include””的用法。问题完美解决!…

    2022年6月16日
  • 操作系统银行家算法C语言代码实现「建议收藏」

    操作系统银行家算法C语言代码实现「建议收藏」计算机操作系统课设需要,写了两个下午的银行家算法(陷在bug里出不来耽误了很多时间),参考计算机操作系统(汤子瀛)实现过程中不涉及难度较大的算法,仅根据银行家算法的思想和步骤进行实现。以下为详细步骤

    2022年7月4日
  • 匿名函数调用方法_javascript匿名函数

    匿名函数调用方法_javascript匿名函数首先看一下普通函数和匿名函数的区别//普通函数functionsum(a,b){ returna+b; console.log(“我是一个普通函数”) }//匿名函数,不能单独使用function(a,b){ returna+b; console.log(“我是一个匿名函数”) }没错,匿名函数简单来说就是普通函数去掉名字,但是他不能单独定义与使用,下面是匿名函数的一些使用场景:用于函数表达式、作为返回值、用于定义对象方法、作为回调函数、用于立即执行函数、用于DOM元素注

    2022年10月3日

发表回复

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

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