《数据结构与算法分析 java语言描述》-- java 表List的删除操作,时间复杂度对比
发布日期:2021-06-30 16:57:14 浏览次数:3 分类:技术文章

本文共 1287 字,大约阅读时间需要 4 分钟。

package cn.edut.test_list;import java.util.*;public class Demo_removeEvensVer1 {
public static void main(String[] args) {
Integer[] a = {
6,5,1,4,2}; //算法一测试 System.out.println(removeEvensVer1(createArrayList(a)).toString()); //算法二测试 try {
System.out.println(removeEvensVer2(createArrayList(a)).toString()); }catch (Exception e) {
// TODO: handle exception System.out.println("算法二:迭代器非法"); } //算法三测试 System.out.println(removeEvensVer3(createArrayList(a)).toString()); } public static
List
createArrayList(T[] a) {
List
lst = new LinkedList<>(); for(int i=0 ; i
List
removeEvensVer1(List
lst) { int i=0 ; while( i < lst.size()) { if(lst.get(i)%2==0) { lst.remove(i); }else { i++; } } return lst; } /** * 第二种算法 (这里的迭代器是非法的!会报错) * 删除偶数 * (如果不报错,他们的时间复杂度分别为:) * ArrayList O(N^2) * LinkedList O(N) * @param lst */ public static
List
removeEvensVer2(List
lst) { for(T i : lst) { //因为有删除操作,这个是非法的 if(i%2==0) { lst.remove(i) ; } } return lst ; } /** * 第三种算法 * 删除偶数 * * * * @param lst */ public static
List
removeEvensVer3(List
lst) { Iterator
iterator = lst.iterator() ; while(iterator.hasNext()) { T temp = iterator.next(); if(temp%2==0) { iterator.remove(); } } return lst; }}

转载地址:https://lawsssscat.blog.csdn.net/article/details/102932463 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java培优班-第五天 - 基础语法 - (数组+变量+OOP1)
下一篇:java 注解@Test的简单用法、测试 - 单元测试方法

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月05日 01时03分23秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

【深度学习笔记】组卷积 2019-04-30
【深度学习笔记】循环神经网络和递归神经网络区别 2019-04-30
【学习笔记】英文科技论文常见英语句式积累 2019-04-30
【深度学习笔记】PixelShuffle 2019-04-30
【python3学习笔记】斜杠和双斜杠运算符的区别 2019-04-30
【深度学习笔记】torch.nn.Sequential(* args) 与 torch.nn.Module 2019-04-30
【深度学习笔记】用torch.nn.Sequential()搭建神经网络模型 2019-04-30
【深度学习笔记】用torch.nn.ModuleList搭建神经网络 2019-04-30
【解决错误】AttributeError: module ‘scipy.misc‘ has no attribute ‘imread‘ 2019-04-30
【解决错误】复现RCAN的时候遇到了ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’ 2019-04-30
【解决错误】ModuleNotFoundError: No module named ‘skimage‘ 2019-04-30
【深度学习笔记】pytorch的点乘(dot product) 2019-04-30
【深度学习笔记】残差 2019-04-30
【错误解决】cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\sr 2019-04-30
【python学习笔记】读取指定文件夹中的图片,结合边缘保留滤波EPF 2019-04-30
【工具和环境】Linux下安装pycharm 2019-04-30
【Accumulation】The last two sentences of the abstract 2019-04-30
【Accumulation】The definition of SISR 2019-04-30
【工具与环境】Windows下安装Sublime Text 3 2019-04-30
【解决错误】ValueError: some of the strides of a given numpy array are negative. 2019-04-30