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

栈与队列相关知识(二)

目录

Java中栈(Stack)

一. 常用方法

1.push(E item)

2.pop()

3.peek()

4.empty()

二. 常用方法扩展

1. search(Object o)

2. clone()

3. contains(Object o)

4. size()

5. toArray()

Java中队列(Queue)

一.常用方法(以LinkedList实现Queue为例)

1.add(E e)

2.offer(E e)

3.remove()

4.poll()

5.element()

6.peek()

二. 常用方法扩展(以LinkedList实现Queue为例,同时包括一些Queue接口实现类共有的特性)

1. size()

2. contains(Object o)

3. toArray()

4. clear()

5. iterator()

6. remove(Object o)

7. retainAll(Collection c)


Java中栈(Stack)

一. 常用方法

1.push(E item)

• 功能:将元素item压入栈顶。

• 示例:

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);

2.pop()

• 功能:移除并返回栈顶元素。如果栈为空,会抛出EmptyStackException异常。

• 示例:

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
int topElement = stack.pop();// 返回2

3.peek()

• 功能:返回栈顶元素,但不移除它。如果栈为空,会抛出EmptyStackException异常。

• 示例:

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
int top = stack.peek();// 返回2,栈不变

4.empty()

• 功能:检查栈是否为空,为空返回true,否则返回false。

• 示例:

Stack<Integer> stack = new Stack<>();
boolean isEmpty = stack.empty();// true
stack.push(1);
isEmpty = stack.empty();// false

二. 常用方法扩展

1. search(Object o)

• 功能:返回对象o在栈中的位置,从栈顶开始计数,栈顶元素位置为1。如果对象o不在栈中,则返回 - 1。

• 示例:

Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c");
int position = stack.search("b");// 返回2
int notInStack = stack.search("d");// 返回 - 1

2. clone()

• 功能:创建并返回此栈的副本。克隆后的栈是一个包含相同元素的新栈,但它们在内存中是独立的对象。

• 示例:

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
Stack<Integer> clonedStack = (Stack<Integer>) stack.clone();

3. contains(Object o)

• 功能:判断栈中是否包含指定元素o,如果包含则返回true,否则返回false。

• 示例:

Stack<String> stack = new Stack<>();
stack.push("hello");
boolean hasHello = stack.contains("hello");// 返回true
boolean hasWorld = stack.contains("world");// 返回false

4. size()

• 功能:返回栈中元素的个数。

• 示例:

Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
int size = stack.size();// 返回2

5. toArray()

• 功能:返回一个包含栈中所有元素的数组。数组的顺序与栈中元素的顺序相同,从栈顶到栈底。

• 示例:

Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
Object[] array = stack.toArray();
String[] stringArray = stack.toArray(new String[0]);

Java中队列(Queue)

一.常用方法(以LinkedList实现Queue为例)

1.add(E e)

• 功能:将指定元素e插入队列。如果队列已满(对于有界队列),则抛出IllegalStateException异常。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);

2.offer(E e)

• 功能:将指定元素e插入队列。如果队列已满(对于有界队列),则返回false,与add方法类似但处理满队列的方式不同。

• 示例:

Queue<Integer> queue = new LinkedList<>();
boolean success = queue.offer(1);
success = queue.offer(2);

3.remove()

• 功能:移除并返回队列的头元素。如果队列为空,则抛出NoSuchElementException异常。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int head = queue.remove();// 返回1

4.poll()

• 功能:移除并返回队列的头元素。如果队列为空,则返回null,与remove方法类似但处理空队列的方式不同。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int head = queue.poll();// 返回1

5.element()

• 功能:返回队列的头元素,但不移除它。如果队列为空,则抛出NoSuchElementException异常。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int head = queue.element();// 返回1,队列不变

6.peek()

• 功能:返回队列的头元素,但不移除它。如果队列为空,则返回null,与element方法类似但处理空队列的方式不同。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int head = queue.peek();// 返回1,队列不变

二. 常用方法扩展(以LinkedList实现Queue为例,同时包括一些Queue接口实现类共有的特性)

1. size()

• 功能:返回队列中元素的个数。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
int size = queue.size();// 返回2

2. contains(Object o)

• 功能:判断队列中是否包含指定元素o,如果包含则返回true,否则返回false。

• 示例:

Queue<String> queue = new LinkedList<>();
queue.add("hello");
boolean hasHello = queue.contains("hello");// 返回true
boolean hasWorld = queue.contains("world");// 返回false

3. toArray()

• 功能:返回一个包含队列中所有元素的数组。数组的顺序与队列中元素的顺序相同,从队头到队尾。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
Object[] array = queue.toArray();
Integer[] intArray = queue.toArray(new Integer[0]);

4. clear()

• 功能:移除队列中的所有元素,将队列清空。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
queue.clear();
boolean isEmpty = queue.isEmpty();// 返回true

5. iterator()

• 功能:返回一个迭代器,用于遍历队列中的元素。迭代器按照从队头到队尾的顺序访问元素。

• 示例:

Queue<String> queue = new LinkedList<>();
queue.add("a");
queue.add("b");
Iterator<String> iterator = queue.iterator();
while (iterator.hasNext()) {
    String element = iterator.next();
    System.out.println(element);
}

6. remove(Object o)

• 功能:从队列中移除指定元素o的第一次出现(如果存在)。如果队列为空或者元素不存在,则返回false;如果成功移除元素,则返回true。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
boolean removed = queue.remove(1);

7. retainAll(Collection<?> c)

• 功能:仅保留队列中包含在指定集合c中的元素。如果队列为空或者指定集合c为空,则不做任何操作。如果队列发生了改变(即有元素被移除),则返回true;否则返回false。

• 示例:

Queue<Integer> queue = new LinkedList<>();
queue.add(1);
queue.add(2);
Collection<Integer> collection = Arrays.asList(2, 3);
boolean changed = queue.retainAll(collection);

http://www.kler.cn/news/330046.html

相关文章:

  • LLM基础概念:模型训练
  • 基于SpringBoot的校园健康信息管理系统
  • 相机基础概念
  • 【分布式训练 debug】VS Code Debug 技巧:launch.json实用参数
  • Grafana链接iframe嵌入Web前端一直跳登录页面的问题记录
  • RabbitMQ 延迟消息
  • 51单片机系列-按键检测原理
  • 【CSS3】css开篇基础(1)
  • 算法笔记(五)——分治
  • 【C++】多态(下)
  • C#基础(4)封装——成员方法
  • CSS文本格式化
  • 分层图 的尝试学习 1.0
  • 基于Python的自然语言处理系列(19):基于LSTM的语言模型实现
  • 51单片机的宠物自动投喂系统【proteus仿真+程序+报告+原理图+演示视频】
  • 【代码记录】多线程示例代码
  • C语言+单片机
  • docker -私有镜像仓库 - harbor安装
  • 10.4 Linux_并发_线程
  • 深入探讨 Docker:远程登录与镜像管理
  • C++容器之list基本使用
  • 上海我店:创新模式引领本地生活新风尚
  • c#使用winscp库实现FTP/SFTP/SCP的获取列表、上传和下载功能
  • 大数据比懂知识点:Parquet、ORC还是Avro作为数据存储格式,哪种在性能和压缩率上更优
  • 【C++二分查找 前缀和】1712. 将数组分成三个子数组的方案数|2078
  • 深入解析开源大模型的GPU资源需求与优化策略
  • 程序员如何通过专业与软技能提升核心竞争力
  • 特权访问管理阻力最小的途径
  • 付费计量系统通用功能(9)
  • 企望制造ERP系统存在RCE漏洞