在開始學習棧之前,先來解決一下之前一個網友在評論區問的問題:
Collection和Collections有什么區別?
雖然這兩個類都在java.util包下,雖然只有一字之差,但它們的差別還是挺大的!
Collection 是JDK中集合層次結構中的最根本的接口。定義了集合類的基本方法。源碼中的解釋:
* The rootinterfaceinthe collection hierarchy.? A collection
* represents a group of objects, known as its <i>elements</i>.? Some
* collections allow duplicate elements and othersdonot.? Some are ordered
* and others unordered.? The JDK does not provide any <i>direct</i>
* implementations ofthisinterface: 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 集合框架的工具類。源碼中的解釋:
* Thisclassconsistsexclusively ofstaticmethods that operate on orreturn
* collections.? It contains polymorphic algorithms that operate on
* collections,"wrappers", whichreturnanewcollectionbacked 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 stacks =newStack<>();
//push方法入棧
stacks.push("開");
stacks.push("工");
stacks.push("大");
stacks.push("吉");
stacks.push("!");
System.out.println(stacks);
//pop棧頂元素出棧
Stringpop=stacks.pop();
System.out.println(pop);
//查看棧頂元素
Stringpeek=stacks.peek();
System.out.println(peek);
//判斷堆棧是否為空
booleanempty=stacks.empty();
System.out.println(empty);
//查看元素在堆棧中的位置
intindex=stacks.search("開");
System.out.println(index);
輸出:
[開, 工, 大, 吉, !]
!
吉
false
4
手寫一個stack(堆棧)
通過上面的代碼示例我們了解了一個棧所具備的功能特點,根據它的特點,我們嘗試一下手寫一個棧!
首先,準備一個數組用來存儲元素,可以定義為Object,這樣支持多數據類型,我們這里直接選用int類型的好嘞。
自定義棧-源碼:
/**
*@ClassNameStack
*@Description手寫一個int類型的堆棧
*@Authorhzm
*@Date2024/2/18 14:21
*@Version1.0
*/
publicclassStack{
privateintarr[];
privateinttop;
privateintcapacity;
/**
? ? * 提供一個有參構造,初始化棧
*@paramsize
? ? */
publicStack(intsize){
this.arr =newint[size];
this.top = -1;
this.capacity = size;
? ? }
/**
? ? * 入棧
*@paramp
? ? */
publicvoidpush(intp){
if(isFull()) {
System.out.println("堆棧空間溢出\n程序終止\n");
System.exit(1);
? ? ? ? }
System.out.println("入棧:"+ p);
? ? ? ? arr[++top] = p;
? ? }
/**
? ? * 出棧
*@return
? ? */
publicintpop(){
if(isEmpty()) {
System.out.println("空棧,不可POP");
System.exit(1);
? ? ? ? }
returnarr[top--];
? ? }
/**
? ? * 判斷棧是否已滿
*@return
? ? */
publicBooleanisFull(){
returntop== capacity -1;
? ? }
/**
? ? * 判斷棧是否為空
*@return
? ? */
publicBooleanisEmpty(){
returntop== -1;
? ? }
/**
? ? * 遍歷棧內元素
? ? */
publicvoidprintStack(){
for(inti=0; i <= top; i++) {
? ? ? ? ? ? System.out.println(arr[i]);
? ? ? ? }
? ? }
/**
? ? * 返回棧的大小
*@return
? ? */
publicintsize(){
returntop +1;
? ? }
/**
? ? * 查看棧頂元素
*@return
? ? */
publicvoidpeek(){
System.out.println("棧頂元素:"+ arr[top]);
? ? }
}
測試類中調用手寫的這個stack:
publicclassTest{
publicstaticvoidmain(String[] args){
Stackstack=newStack(5);
//入棧
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
//出棧
intpop=stack.pop();
System.out.println("出棧:"+ pop);
//查看棧的大小
intsize=stack.size();
System.out.println("棧容量:"+ size);
//查看棧頂元素
? ? ? ? stack.peek();
//打印棧內元素
? ? ? ? stack.printStack();
? ? }
}
輸出:
散熱風扇https://www.uv-semi.com/
深圳網站建設www.sz886.com