Collection和Collections的区别?

在开始学习栈之前,先来解决一下之前一个网友在评论区问的问题:

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容