Java 核心技术学习笔记— Java集合框架部分内容#yyds干货盘点#

举报
宇宙之一粟 发表于 2022/01/14 23:04:52 2022/01/14
【摘要】 Java 最初版本只提供了最初的几个 Java 集合框架个类: VectorStackHashableBitSetEnumeration 其中 Enumeration 接口提供了一种用于访问任意容器中各个元素的抽象机制。 Java 集合类库将接口( interface )与实现(i...

Java 最初版本只提供了最初的几个 Java 集合框架个类:

  • Vector
  • Stack
  • Hashable
  • BitSet
  • Enumeration

其中 Enumeration 接口提供了一种用于访问任意容器中各个元素的抽象机制。

Java 集合类库将接口( interface )与实现(implementation)分离。

队列是如何分离的

队列涉及的几个操作:

  • 在队尾添加元素
  • 在队头删除元素
  • 查找队列中元素的个数

特点:先入先出

队列的接口:

public interface Queue<E> // a simplified form of the interface in the stardard library{    void add(E element);    E remove();    int size();}
        

在数据结构课中,队列通常有两种实现方式:

  1. 使用循环数组;但这种方式的局限是队列的长度有限。
  2. 使用链表

代码表示如下:

public class CircularArrayQueue<E> implements Queue<E>  // not an actual library class{    private int head;    private int tail;        CircularArrayQueue(int capacity) {...}        public void add(E element) {...}    public E remove() {...}    public int size() {...}    private E[] elements;}public class LinkedListQueue<E> implements Queue<E>  // not an actual library class{    private Link head;    private Link tail;        LinkedListQueue() {...}    public void add(E element) {...}    public E remove() {...}    public int size() {...}}
        

注释:实际上,Java 类库没有名为 CirclularArrayQueue 和 LinkedListQueue 的类。这里只是以这些类作为示例来解释集合接口与实现在概念上的区分。

Queue 的使用

假设定义了上述的 ​​CircularArrayQueue​​​ 和 ​​LinkedListQueue​​ 之后,就可以直接使用:

Queue<Customer> expressLane = new CircularArrayQueue<>(100);expressLane.add(new Customer("Harry")
        
Queue<Customer> expressLane = new LinkedListQueue<>();expressLane.add(new Customer("Bug")
        

循环数组要比链表高效,因此多数人优先选择循环数组。

但是循环数组是一个有界数组,即容量有限。如果使用的对象数量没有上限,最好使用链表实现。

文章来源: blog.csdn.net,作者:宇宙之一粟,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/yuzhou_1shu/article/details/122476798

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。