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)
上一篇 2022年8月11日 下午6:00
下一篇 2022年8月11日 下午6:00


相关推荐

  • jQuery cdn加速

    jQuery cdn加速jquery 1 11 0 min jscdnjQuery scriptsrc http libs baidu com jquery 2 0 0 jquery min js 百度 CDN 支持版本 2 0 3 2 0 2 2 0 1 2 0 0 1 11 1 1 10 2 1 10 1 1 10 0 1 9 1 1 9 0 1 8 3 1 8 2 1 8 1 1 8 0 1 7 2 1 7 1 1 7 0 scriptsrc http

    2026年3月18日
    2
  • 放弃UI,接管底层:Obsidian Skills 如何让 AI Agent 真正“骇”入本地知识库

    放弃UI,接管底层:Obsidian Skills 如何让 AI Agent 真正“骇”入本地知识库

    2026年3月14日
    2
  • 前端工程师vscode必备插件(20个)

    前端工程师vscode必备插件(20个)阶段:前端新手只会html、css、js1.Chinese汉化vscode2.TokyoNightMaterialTheme已经下架了,这个是目前来说个人认为vscode中最好看的主题。3.vscode-icons文件的图标,这个是看着最顺眼的图标。4.prettier代码格式化工具,代码自动格式化。(需配置,最下面放上代码)如果安装了vetur,则会产生冲突,需要手动右键格式化,选择prettier。5.openinbrowser打开浏览器插件。

    2022年7月25日
    16
  • python 通达信公式函数,python通达信公式函数,python调用通达信数据

    python 通达信公式函数,python通达信公式函数,python调用通达信数据内容导航 Q1 这样的循环可否用通达信公式中的某个函数来实现 就是一直取最低 LLV X 0 比如 LLV L 0 而我的本意是数值不会上升 只会下降 上面的公式你试试看没 LLV L 0 就

    2026年3月19日
    2
  • Python – pycharm 代码自动补全

    Python – pycharm 代码自动补全安装 pycharm 后 输入代码后 没有补全提示首先检查是否关闭了代码提示 如下图 将红框中 PowerSaveMod 前的勾去掉第二步 如果在输入某些代码时还是没有补全提醒 可能是配置好 python 环境则点击 file gt settings gt projectInter 如下图选择安装的 python 输入代码就会有提示了

    2026年3月19日
    3
  • python爬虫全解

    python爬虫全解一、爬虫基础简介什么是爬虫:-通过编写程序,模拟浏览器上网,然后让其去互联网上抓取数据的过程。爬虫的价值:-实际应用-就业爬虫究竟是合法还是违法的?-在法律中是不被禁止-具有违法风险-

    2022年7月3日
    25

发表回复

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

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