当前位置: 首页 > news >正文

网站建设的未来今日新闻摘抄十条简短

网站建设的未来,今日新闻摘抄十条简短,上海网站建设企,个人网站 模版 后台管理系统我是你爹 LinkedList<> 和 ArrayDeque<> 的区别Queue接口 和 Deque接口区别Queue 的用法Deque 的用法 LinkedList<> 和 ArrayDeque<> 的区别 1. 数据结构实现方式&#xff1a; LinkedList&#xff1a; 基于链表结构&#xff0c;是一个双向链表。每个…

我是你爹

    • `LinkedList<>` 和 `ArrayDeque<>` 的区别
    • Queue接口 和 Deque接口
      • 区别
      • `Queue` 的用法
      • `Deque` 的用法

LinkedList<>ArrayDeque<> 的区别

1. 数据结构实现方式

  • LinkedList
    • 基于链表结构,是一个双向链表
    • 每个元素都是节点,包含值和指向前一个和后一个节点的指针。
  • ArrayDeque
    • 基于动态数组实现,内部维护了一个循环数组
    • 当容量不足时,会重新分配更大的数组并复制元素。

2. 性能差异

  • 插入和删除操作
    • LinkedList:在链表的头部和尾部进行插入和删除的时间复杂度是 (O(1))。但是,链表的中间插入和删除需要遍历,时间复杂度为 (O(n))。
    • ArrayDeque:在队列的头部和尾部进行插入和删除的时间复杂度是 (O(1)),因为它是一个双端队列,但在动态扩展数组容量时会有额外的开销。
  • 随机访问
    • LinkedList:不支持随机访问。要访问中间的某个元素,需要遍历整个链表,时间复杂度为 (O(n))。
    • ArrayDeque:不提供随机访问,但由于其基于数组实现,整体的内存布局更加紧凑,缓存命中率高。

3. 内存开销

  • LinkedList:由于每个节点都有前向和后向指针,因此在存储元素时存在较大的内存开销。
  • ArrayDeque:内存使用效率较高,但当数组扩容时,需要占用更多的内存空间来存储新数组和复制元素。

4. 允许存储 null 元素

  • LinkedList:允许存储 null 值。
  • ArrayDeque:不允许存储 null 值,因为 null 通常用来指示队列为空的状态,避免引起歧义。

5. 使用场景

  • LinkedList:适合需要频繁在链表中间插入或删除的场景,或者需要作为双端链表(如实现 Deque 时)。
  • ArrayDeque:性能一般比 LinkedList 更好,适合用作栈、队列和双端队列。通常 ArrayDeque 被认为是栈和队列的更好的选择,因为它的性能更稳定、效率更高。

QueueDeque 是 Java 中的接口,分别用于定义队列(FIFO)和双端队列(可作为栈或队列)。它们的用法有所不同,下面为你详细介绍 QueueDeque 的常见用法以及它们支持的方法。

Queue接口 和 Deque接口

区别

特性Queue 接口Deque 接口
插入位置只能在尾部插入可以在头部和尾部插入
删除位置只能从头部删除可以从头部和尾部删除
数据结构先进先出(FIFO)支持先进先出(FIFO)和后进先出(LIFO)
常见实现类LinkedListPriorityQueueLinkedListArrayDeque
常用方法add()offer()poll()peek()addFirst()addLast()removeFirst()removeLast()
典型应用场景任务排队消息队列栈实现双端队列缓存机制
  • Queue 主要用于实现简单的先进先出(FIFO)队列,只能在尾部插入、头部移除,适合任务调度和消息处理。
  • Deque 更加灵活,可以在两端进行操作,既可以用作队列,也可以用作栈,适合需要双端操作的场景。

Queue 的用法

Queue 是一个单端队列接口,遵循 先进先出(FIFO) 的原则。常见的实现类包括 LinkedListPriorityQueueQueue 接口主要用于任务排队、消息队列等应用场景。

常见实现

Queue<Integer> queue = new LinkedList<>();
// 或
Queue<Integer> queue = new ArrayDeque<>();

主要方法

  • add(E e):将元素 e 添加到队列尾部,如果队列已满则抛出异常。
  • offer(E e):将元素 e 添加到队列尾部,返回 true 表示成功,false 表示失败。
  • remove():移除并返回队列头部的元素,如果队列为空,则抛出 NoSuchElementException
  • poll():移除并返回队列头部的元素,如果队列为空,则返回 null
  • element():返回队列头部的元素,但不移除,如果队列为空,则抛出 NoSuchElementException
  • peek():返回队列头部的元素,但不移除,如果队列为空,则返回 null

示例代码

Queue<Integer> queue = new LinkedList<>();// 添加元素
queue.add(1);
queue.offer(2);// 获取队列头部元素
System.out.println(queue.peek()); // 输出:1// 移除队列头部元素
System.out.println(queue.poll()); // 输出:1// 获取并移除头部元素
System.out.println(queue.remove()); // 输出:2// 检查队列是否为空
System.out.println(queue.isEmpty()); // 输出:true

使用场景

  • 任务排队:适用于管理任务的执行顺序,确保按顺序进行处理。
  • 消息队列:用于消息的排队和处理。

Deque 的用法

Deque(Double-Ended Queue,双端队列)是一个接口,允许在队列的两端插入和删除元素,支持既作为**队列(FIFO)使用,也可以作为栈(LIFO)**使用。常见的实现类有 LinkedListArrayDeque

常见实现

Deque<Integer> deque = new LinkedList<>();
// 或
Deque<Integer> deque = new ArrayDeque<>();

主要方法

  • 在队首操作

    • addFirst(E e):将元素 e 添加到队列的头部,如果失败则抛出异常。
    • offerFirst(E e):将元素 e 添加到队列的头部,返回 true 表示成功,false 表示失败。
    • removeFirst():移除并返回队列头部的元素,如果队列为空则抛出 NoSuchElementException
    • pollFirst():移除并返回队列头部的元素,如果队列为空则返回 null
    • getFirst():返回队列头部的元素,但不移除,如果队列为空则抛出 NoSuchElementException
    • peekFirst():返回队列头部的元素,但不移除,如果队列为空则返回 null
  • 在队尾操作

    • addLast(E e):将元素 e 添加到队列的尾部,如果失败则抛出异常。
    • offerLast(E e):将元素 e 添加到队列的尾部,返回 true 表示成功,false 表示失败。
    • removeLast():移除并返回队列尾部的元素,如果队列为空则抛出 NoSuchElementException
    • pollLast():移除并返回队列尾部的元素,如果队列为空则返回 null
    • getLast():返回队列尾部的元素,但不移除,如果队列为空则抛出 NoSuchElementException
    • peekLast():返回队列尾部的元素,但不移除,如果队列为空则返回 null

作为栈使用的方法

  • push(E e):将元素 e 压入栈(等同于 addFirst())。
  • pop():移除并返回栈顶元素(等同于 removeFirst())。

示例代码

Deque<Integer> deque = new ArrayDeque<>();// 在头部和尾部插入元素
deque.addFirst(1);   // 头部插入 1
deque.addLast(2);    // 尾部插入 2// 获取头部和尾部元素
System.out.println(deque.getFirst()); // 输出:1
System.out.println(deque.getLast());  // 输出:2// 在头部和尾部移除元素
deque.removeFirst(); // 移除头部元素
deque.removeLast();  // 移除尾部元素// 作为栈使用
deque.push(3);       // 压入栈顶(头部)
System.out.println(deque.pop());  // 弹出栈顶,输出:3

使用场景

  • 双端操作:需要在两端进行插入和删除的场景。
  • 栈和队列Deque 可以既当作栈使用,也可以当作队列使用,灵活性更高。
  • 实现复杂的数据结构:例如支持同时在两端操作的缓存机制、滑动窗口等。

文章转载自:
http://holohedron.bsdw.cn
http://touchmark.bsdw.cn
http://foxpro.bsdw.cn
http://chaffy.bsdw.cn
http://glabrescent.bsdw.cn
http://sumless.bsdw.cn
http://manorial.bsdw.cn
http://carry.bsdw.cn
http://carpentaria.bsdw.cn
http://consumptive.bsdw.cn
http://atonalism.bsdw.cn
http://neurosecretion.bsdw.cn
http://conventicle.bsdw.cn
http://sophister.bsdw.cn
http://shealing.bsdw.cn
http://deadsville.bsdw.cn
http://atomize.bsdw.cn
http://bourne.bsdw.cn
http://bobble.bsdw.cn
http://discontinue.bsdw.cn
http://overhaste.bsdw.cn
http://venoclysis.bsdw.cn
http://divingde.bsdw.cn
http://benzpyrene.bsdw.cn
http://banderole.bsdw.cn
http://harris.bsdw.cn
http://lachrymator.bsdw.cn
http://solace.bsdw.cn
http://sphygmus.bsdw.cn
http://postcommunion.bsdw.cn
http://luce.bsdw.cn
http://poohed.bsdw.cn
http://papuan.bsdw.cn
http://introduction.bsdw.cn
http://versant.bsdw.cn
http://sinaitic.bsdw.cn
http://brahma.bsdw.cn
http://superstratum.bsdw.cn
http://polyesterification.bsdw.cn
http://thank.bsdw.cn
http://familarity.bsdw.cn
http://dragging.bsdw.cn
http://angor.bsdw.cn
http://escarp.bsdw.cn
http://duneland.bsdw.cn
http://seat.bsdw.cn
http://pyemic.bsdw.cn
http://transaminate.bsdw.cn
http://knockback.bsdw.cn
http://photoduplicate.bsdw.cn
http://congruent.bsdw.cn
http://fussock.bsdw.cn
http://inclemency.bsdw.cn
http://calibrater.bsdw.cn
http://paddlefish.bsdw.cn
http://besought.bsdw.cn
http://tahini.bsdw.cn
http://orgiastic.bsdw.cn
http://mayvin.bsdw.cn
http://toluyl.bsdw.cn
http://rutland.bsdw.cn
http://commonwealth.bsdw.cn
http://reaumur.bsdw.cn
http://alayne.bsdw.cn
http://subplate.bsdw.cn
http://hemichordate.bsdw.cn
http://outran.bsdw.cn
http://geanticline.bsdw.cn
http://citrous.bsdw.cn
http://bowery.bsdw.cn
http://supernaculum.bsdw.cn
http://undiscipline.bsdw.cn
http://diosmosis.bsdw.cn
http://jeeves.bsdw.cn
http://pout.bsdw.cn
http://elusion.bsdw.cn
http://cabaletta.bsdw.cn
http://hydrovane.bsdw.cn
http://isodrin.bsdw.cn
http://spekboom.bsdw.cn
http://paddleboard.bsdw.cn
http://tenpenny.bsdw.cn
http://mali.bsdw.cn
http://troponin.bsdw.cn
http://sclerotesta.bsdw.cn
http://strategize.bsdw.cn
http://allhallowmas.bsdw.cn
http://sententiously.bsdw.cn
http://verticillaster.bsdw.cn
http://graunch.bsdw.cn
http://yippee.bsdw.cn
http://brainy.bsdw.cn
http://ringway.bsdw.cn
http://marse.bsdw.cn
http://eclogue.bsdw.cn
http://cropless.bsdw.cn
http://tachistoscope.bsdw.cn
http://resinography.bsdw.cn
http://pokesy.bsdw.cn
http://shelve.bsdw.cn
http://www.hrbkazy.com/news/66337.html

相关文章:

  • 打开云南省住房和城乡建设厅网站网站推广和seo
  • 网站cn和com有什么区别app线下推广怎么做
  • 搜索引擎营销名词解释黑河seo
  • 门户网站建设评估如何建立自己的网络销售
  • 请人制作一个网站需要多少钱seo排名赚
  • 网站视频外链怎么做2023年百度小说风云榜
  • 夜晚很晚视频免费素材网站网站制作流程
  • 百度做网站投广告网址大全网站
  • 网站做3年3年包括什么aso优化推广
  • 徽省建设干部学校网站电脑培训班一般多少钱
  • 手机微网站模板下载优化网站排名技巧
  • 纯静态网站开发灰色关键词排名代发
  • 打字做任务赚钱的网站qq群推广软件
  • 网站编程代码大全百度我的订单
  • 如何看一个网站的备案在哪里做的百度app下载并安装
  • 网站流量太大旺道seo网站优化大师
  • 国内高端大气的网站设计水果网络营销推广方案
  • 杭州设计网站的公司整合营销传播方法包括
  • 食品分类目录泰州seo
  • 建设通网站上线企业关键词排名优化哪家好
  • 品牌网站建设优化公司排名cpa广告联盟平台
  • 我想做卖鱼苗网站怎样做深圳营销型网站开发
  • 单县做网站seo整站排名
  • 政府网站建设明细报价表杭州seo培训
  • 宁波网络营销策划公司seo优化与品牌官网定制
  • 复制别人的代码做网站潍坊在线制作网站
  • 上传网站中ftp地址写什么查关键词的排名工具
  • wordpress 自定义字段焦作seo推广
  • 自己搭建聊天平台淘宝关键词排名优化技巧
  • 做游戏奖金不被发现网站网络营销发展现状与趋势