

Java集合篇之逐渐被遗忘的Stack,手写一个栈你会吗? - JavaBuild
source link: https://www.cnblogs.com/JavaBuild/p/18020340
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

正月初九,开工大吉!
2024年,更上一层楼!
其实在List的继承关系中,除了ArrayList和LinkedList之外,还有另外一个集合类stack(栈),它继承自vector,线程安全,先进后出,随着Java并发编程的发展,它在很多应用场景下被逐渐替代,成为了Java的遗落之类。不过,stack在数据结构中仍有一席之地,因此,我们有必要也应该好好的学一下!
Collection和Collections的区别?
在开始学习栈之前,先来解决一下之前一个网友在评论区问的问题:
Collection和Collections有什么区别?
虽然这两个类都在java.util包下,虽然只有一字之差,但它们的差别还是挺大的!
Collection 是JDK中集合层次结构中的最根本的接口。定义了集合类的基本方法。源码中的解释:
* The root interface in the <i>collection hierarchy</i>. A collection * represents a group of objects, known as its <i>elements</i>. Some * collections allow duplicate elements and others do not. Some are ordered * and others unordered. The JDK does not provide any <i>direct</i> * implementations of this interface: it provides implementations of more * specific subinterfaces like <tt>Set</tt> and <tt>List</tt>. This interface * is typically used to pass collections around and manipulate them where * maximum generality is desired.
Collections 是一个包装类。它包含有各种有关集合操作的静态多态方法,不能实例化,Collection 集合框架的工具类。源码中的解释:
* This class consists exclusively of static methods that operate on or return* collections. It contains polymorphic algorithms that operate on* collections, "wrappers", which return a new collection backed by a* specified collection, and a few other odds and ends.
stack(栈)
栈(stack)是一种先进后出(Last In First Out,LIFO)的数据结构,类比于现实生活中的子弹上膛、泡泡圈。栈具有两个基本操作:入栈(push)和出栈(pop)。入栈表示将元素放入栈顶,而出栈表示从栈顶取出元素。
动图图解-入栈(push)

动图图解-出栈(pop)

在Java的工具包中其实帮我们封装好了一个类,java.util.Stack,它所提供的方法并不多,我们通过一个小示例感受一下。
【代码示例1】
Stack<String> stacks = new Stack<>();//push方法入栈 stacks.push("开"); stacks.push("工"); stacks.push("大"); stacks.push("吉"); stacks.push("!"); System.out.println(stacks); //pop栈顶元素出栈 String pop = stacks.pop(); System.out.println(pop); //查看栈顶元素 String peek = stacks.peek(); System.out.println(peek); //判断堆栈是否为空 boolean empty = stacks.empty(); System.out.println(empty); //查看元素在堆栈中的位置 int index = stacks.search("开"); System.out.println(index);
输出:
[开, 工, 大, 吉, !]!吉false4
手写一个stack(堆栈)
通过上面的代码示例我们了解了一个栈所具备的功能特点,根据它的特点,我们尝试一下手写一个栈!
首先,准备一个数组用来存储元素,可以定义为Object,这样支持多数据类型,我们这里直接选用int类型的好嘞。自定义栈-源码:
/** * @ClassName Stack * @Description 手写一个int类型的堆栈 * @Author hzm * @Date 2024/2/18 14:21 * @Version 1.0 */public class Stack { private int arr[]; private int top; private int capacity; /** * 提供一个有参构造,初始化栈 * @param size */ public Stack(int size) { this.arr = new int[size]; this.top = -1; this.capacity = size; } /** * 入栈 * @param p */ public void push(int p) { if (isFull()) { System.out.println("堆栈空间溢出\n程序终止\n"); System.exit(1); } System.out.println("入栈:" + p); arr[++top] = p; } /** * 出栈 * @return */ public int pop() { if (isEmpty()) { System.out.println("空栈,不可POP"); System.exit(1); } return arr[top--]; } /** * 判断栈是否已满 * @return */ public Boolean isFull() { return top == capacity - 1; } /** * 判断栈是否为空 * @return */ public Boolean isEmpty() { return top == -1; } /** * 遍历栈内元素 */ public void printStack() { for (int i = 0; i <= top; i++) { System.out.println(arr[i]); } } /** * 返回栈的大小 * @return */ public int size() { return top + 1; } /** * 查看栈顶元素 * @return */ public void peek(){ System.out.println("栈顶元素:" + arr[top]); }}
测试类中调用手写的这个stack:
public class Test { public static void main(String[] args) { Stack stack = new Stack(5); //入栈 stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); //出栈 int pop = stack.pop(); System.out.println("出栈:"+ pop); //查看栈的大小 int size = stack.size(); System.out.println("栈容量:" + size); //查看栈顶元素 stack.peek(); //打印栈内元素 stack.printStack(); }}
输出:
入栈:1入栈:2入栈:3入栈:4入栈:5出栈:5栈容量:4栈顶元素:41234
好了,今天的栈内容就写到这里吧,大家私下里可以找找leetcode上关于栈的算法题做做,深刻感受一下哈!
如果本篇博客对您有一定的帮助,大家记得留言+点赞+收藏呀。原创不易,转载请联系Build哥!

Recommend
-
6
大家好,我是冰河~~说起Java,简单好用,但是Java中很多牛逼的技术却逐渐被遗忘了~~在Java语言出现之前,很多系统都是使用C和C++开发的。Java出现之后,由于其面向对象的思想更加符合人们的思维习惯,Java也不...
-
3
注意!这种笔试方式正在逐渐被取代…… showmebug · 1天之前 · 186 次点击 · 预计阅读时间 1 分钟...
-
9
小屏产品逐渐被放弃:苹果宣布11英寸MacBook Air退场 2022-04-02 09:...
-
8
逐渐被黑客盯上的DeFi 01区块链 刚刚 知名链游Axie Infinity被黑损失了6.25亿美元,巨星周杰伦的NFT也被盗了。近期,针对DeFi领域的黑客事件正在激增...
-
6
逐渐被黑客盯上的 DeFi,还能安全吗? 01区块链 原创 2022-04-08 01:30 热度 211009 分享 微信扫一扫:分享 ...
-
7
之前更新了不少Java的基础知识,比如Java的类、对象、基础类型、关键字、序列化、泛型、值传递等等,今天要上点深度了,来聊一聊Java中的 反射 ! 所谓反射,就是在运行时分析、检查和操作类、接口、方法、...
-
9
在《深入剖析Java中的反射,由浅入深,层层剥离!》这篇文章中我们讲反射时,曾提到过Java的动态代理中使用了反射技术,那么好,今天我们要就着反射的索引,来学习一下Java中的代理!
-
2
Promise, async, await实现异步编程,代码详解
-
6
在Java的世界里万物皆对象。但我认为是万物皆数据,世界由各种各样数据构建起来,我们通过程序去实现数据的增删改查、转入转出、加减乘除等等,不同语言的实现方式殊途同归。由此可见,数据对于程序语言的重要性。 这段话是在写数据类型那篇博客时说的,当时...
-
0
Java集合篇之深入解析LinkedList - JavaBuild - 博客园
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK