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


相关推荐

  • ​git拉取远程分支到本地 ​

    ​git拉取远程分支到本地 ​使用如下 git 命令查看所有远程分支 gitbranch r 方法一使用如下命令 gitcheckout b 本地分支名 xorigin 远程分支名 x 使用该方式会在本地新建分支 x 并自动切换到该本地分支 x 方式二使用如下命令 gitfetchorig 远程分支名 x 本地分支名 x 使用该方式会在本地新建分支 x 但是不会自动切换到该本地分支 x 需要手动 checkout gitbranchset upstream toorigin 远程分支名本地分支名参见博文 Git

    2025年9月1日
    3
  • 用了vue还需要jquery吗_vue与react的区别

    用了vue还需要jquery吗_vue与react的区别⾸先呢jquery他是⽤js封装的⼀个类库,主要是为了⽅便操作dom元素,⽽vue他是⼀个框架,并且呢,他会从真实dom构建出⼀个虚拟的dom树,通过di!算法渲染只发⽣改变的dom元素,其他的相同的dom元素不⽤在重新渲染.⽽使⽤jquery去改变dom元素的时候,即使有相同的dom元素也会重新渲染,jq重点操作dom,而vue重点操作数据。以上就是我对vue和jquery区别的理解….

    2022年10月15日
    2
  • Codeblocks中文乱码解决方法。[通俗易懂]

    Codeblocks中文乱码解决方法。[通俗易懂]Codeblocks中文乱码解决方法。 如需安装包请后台留言!!Codeblocks中文乱码解决方法:特别提示:出现中文乱码情况才执行以下操作,未出现请勿随意修改!!!!打开Codeblocks-&gt;设置-&gt;编辑器:然后点击Encodingsettings-&gt;选择编码-&gt;选择UTF-8-…

    2022年7月14日
    20
  • C++多线程编程:同步之互斥量Mutex「建议收藏」

    C++多线程编程:同步之互斥量Mutex「建议收藏」文章目录5.示例代码文章目录1.CreateMutex()2.ReleaseMutex()3.WaitForSingleobject()4.CloseHandle()5.示例代码6.Mutex实现一个程序只允许允许一个实例(进程)5.示例代码文章目录1.CreateMutex()2.ReleaseMutex()3.WaitForSingleobject()4.CloseHandle()5.示例代码6.Mutex实现一个程序只允许允许一个实例(进程))5.示例代码文章目录1

    2022年6月26日
    37
  • [bzoj3884] 上帝与集合的正确用法

    [bzoj3884] 上帝与集合的正确用法

    2022年3月7日
    35
  • 计算机组成原理知识点

    计算机组成原理知识点计算机体系结构(ComputerArchitecture)主要研究硬件和软件功能的划分,确定硬件和软件的界面,哪部分功能由硬件系统来完成,哪部分功能由软件系统来完成。计算机组成原理(ComputerOrganization)是依据计算机体系结构,在确定且分配了硬件子系统的概念结构和功能特性的基础上,设计计算机各部件的具体组成,以及它们之间的连接关系,实现机器指令级的各种功能和特性,这点上说

    2022年6月1日
    55

发表回复

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

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