Java容器Stack

Stack继承关系

Collection  接口AbstractCollectionAbstractListVectorStack

方法

public E push 元素在栈顶,最后一个元素
public synchronized E pop() 删除并返回栈顶元素(最后一个)
public synchronized E peek()返回栈顶元素(最后一个)
public synchronized int search(Object o)返回元素所在位置(反向位置)
public boolean empty() 是否为空

常用方法测试


import java.util.Iterator;
import java.util.List;
import java.util.Stack;import lombok.extern.slf4j.Slf4j;@Slf4j
public class StackTest {public static void main(String[] args) {Stack stack=new Stack<>();for(int i=0;i<6;i++){stack.push(i);}// 遍历并打印出该栈iteratorStack(stack);// 查找"2"在栈中的位置,并输出log.info("value 2 in {}",stack.search(2));// pop栈顶元素之后,遍历栈log.info("pop {}",stack.pop());iteratorStack(stack);// peek栈顶元素之后,遍历栈log.info("peek {}",stack.peek());iteratorStack(stack);// 通过Iterator去遍历Stack}// 遍历public static void iteratorStack(List list) {Iterator it = list.iterator();while (it.hasNext()) {log.info("{}", it.next());}}
}

输出

2019-07-11 14:45:39,223   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 0
2019-07-11 14:45:39,229   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 1
2019-07-11 14:45:39,229   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 2
2019-07-11 14:45:39,229   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 3
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 4
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 5
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - value 2 in 4
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - pop 5
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 0
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 1
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 2
2019-07-11 14:45:39,230   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 3
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 4
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - peek 4
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 0
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 1
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 2
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 3
2019-07-11 14:45:39,231   [main] INFO  com.fang.java.javabase.firstcollection.StackTest  - 4

源码 openJdk 1.8

/** Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.** This code is free software; you can redistribute it and/or modify it* under the terms of the GNU General Public License version 2 only, as* published by the Free Software Foundation.  Oracle designates this* particular file as subject to the "Classpath" exception as provided* by Oracle in the LICENSE file that accompanied this code.** This code is distributed in the hope that it will be useful, but WITHOUT* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License* version 2 for more details (a copy is included in the LICENSE file that* accompanied this code).** You should have received a copy of the GNU General Public License version* 2 along with this work; if not, write to the Free Software Foundation,* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.** Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA* or visit www.oracle.com if you need additional information or have any* questions.*/package java.util;/*** The <code>Stack</code> class represents a last-in-first-out* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five* operations that allow a vector to be treated as a stack. The usual* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a* method to <tt>peek</tt> at the top item on the stack, a method to test* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>* the stack for an item and discover how far it is from the top.* <p>* When a stack is first created, it contains no items.** <p>A more complete and consistent set of LIFO stack operations is* provided by the {@link Deque} interface and its implementations, which* should be used in preference to this class.  For example:* <pre>   {@code*   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>** @author  Jonathan Payne* @since   JDK1.0*/
public
class Stack<E> extends Vector<E> {/*** Creates an empty Stack.*/public Stack() {}/*** Pushes an item onto the top of this stack. This has exactly* the same effect as:* <blockquote><pre>* addElement(item)</pre></blockquote>** @param   item   the item to be pushed onto this stack.* @return  the <code>item</code> argument.* @see     java.util.Vector#addElement*/public E push(E item) {addElement(item);return item;}/*** Removes the object at the top of this stack and returns that* object as the value of this function.** @return  The object at the top of this stack (the last item*          of the <tt>Vector</tt> object).* @throws  EmptyStackException  if this stack is empty.*/public synchronized E pop() {E       obj;int     len = size();obj = peek();removeElementAt(len - 1);return obj;}/*** Looks at the object at the top of this stack without removing it* from the stack.** @return  the object at the top of this stack (the last item*          of the <tt>Vector</tt> object).* @throws  EmptyStackException  if this stack is empty.*/public synchronized E peek() {int     len = size();if (len == 0)throw new EmptyStackException();return elementAt(len - 1);}/*** Tests if this stack is empty.** @return  <code>true</code> if and only if this stack contains*          no items; <code>false</code> otherwise.*/public boolean empty() {return size() == 0;}/*** Returns the 1-based position where an object is on this stack.* If the object <tt>o</tt> occurs as an item in this stack, this* method returns the distance from the top of the stack of the* occurrence nearest the top of the stack; the topmost item on the* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>* method is used to compare <tt>o</tt> to the* items in this stack.** @param   o   the desired object.* @return  the 1-based position from the top of the stack where*          the object is located; the return value <code>-1</code>*          indicates that the object is not on the stack.*/public synchronized int search(Object o) {int i = lastIndexOf(o);if (i >= 0) {return size() - i;}return -1;}/** use serialVersionUID from JDK 1.0.2 for interoperability */private static final long serialVersionUID = 1224463164541339165L;
}

转载于:https://www.cnblogs.com/JuncaiF/p/11170044.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/410775.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android wifi连接手机,Android手机无线连接利器-AirDroid

AirDroid是一款可以在电脑的浏览器上对手机进行管理的应用&#xff0c;需要wifi网络支持&#xff0c;手机安装启用服务后&#xff0c;在pc的浏览器即可登陆进行管理和操作&#xff0c;可以管理联系人、短信、文件、应用、照片、铃声、音乐、通话记录&#xff0c;还可以快速搜索…

[html] 如何使用纯HTML实现跑马灯的效果?

[html] 如何使用纯HTML实现跑马灯的效果&#xff1f; HTML marquee 元素用来插入一段滚动的文字。 但是该元素已废弃。个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面…

字符串处理

Title#region GetSubString /// <summary> /// 取得指定开始和结束字符串中间的数据串 /// </summary> /// <param name"content"></param> /// <param name"startStr"></param> /// <param name"endSt…

HTTP管线化(HTTP pipelining)

默认情况下http协议中每个传输层连接只能承载一个http请求和响应&#xff0c;然后结束。 HTTP是一个简单的协议。客户进程建立一条同服务器进程的 T C P连接&#xff0c;然后发出请求并读取服务器进程的响应。服务器进程关闭连接表示本次响应结束。服务器进程返回的文件通常…

[html] 如果列表元素li的兄弟元素为div,会产生什么情况?

[html] 如果列表元素li的兄弟元素为div&#xff0c;会产生什么情况&#xff1f; 单纯的对html来说主要是破坏了语义结构吧, css方面来说不好统一控制样式&#xff0c;div默认也没有list-style个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xf…

RFC函数的初步使用-同步

1、由于没有外围系统&#xff0c;采用不同SAP不同client之间进行测试。 首先在A-client搭建需要被调用的RFC函数。在A-client里运行SE37创建函数 在属性页签选择“远程启用的模块” 设定inport参数&#xff0c;传入人员名称去取usr21中的值 设定export参数&#xff0c;其中zper…

C# 繁体,简体互转

首先对Miscrosoft.VisualBasic类的引用. using Microsoft.VisualBasic; public static string Traditional2Simplified(string str) { //繁体转简体 return (Microsoft.VisualBasic.Strings.StrConv(str, Microsoft.VisualBasic.VbStrConv.Si…

[html] html的哪个标签可以预渲染?

[html] html的哪个标签可以预渲染&#xff1f; link 标签的 relpreload个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

2017蓝桥杯c语言C组承压计算,蓝桥杯2017Java B组---分巧克力and承压计算

分巧克力package lala;/**儿童节那天有K位小朋友到小明家做客。小明拿出了珍藏的巧克力招待小朋友们。小明一共有N块巧克力&#xff0c;其中第i块是Hi x Wi的方格组成的长方形。为了公平起见&#xff0c;小明需要从这 N 块巧克力中切出K块巧克力分给小朋友们。切出的巧克力需要…

HDU2138 随机素数测试 Miller-Rabin算法

题目描述 Give you a lot of positive integers, just to find out how many prime numbers there are.. In each case, there is an integer N representing the number of integers to find. Each integer won’t exceed 32-bit signed integer, and each of them won’t be …

[html] 你写一个页面需要多长时间?

[html] 你写一个页面需要多长时间&#xff1f; 和页面结构&#xff0c;样式&#xff0c;交互设计正相关个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与歌谣一起通关前端面试题

Android编程获取手机型号,本机电话号码,sdk版本及firmware版本号(即系统版本号)...

Android开发平台中&#xff0c;可通过TelephonyManager 获取本机号码。 TelephonyManager phoneMgr(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);txtPhoneNumber.setText(phoneMgr.getLine1Number()); //txtPhoneNumber是一个EditText 用于显示手机号注…

vba copy sheet

Sub copySheet() Dim wkbk As Workbook Set wkbk Workbooks.open("源文件.xls") 先打开要复制的文件 wkbk.sheets(1).Copy thisworkbook.sheets(1) 再将此文件中第一个工作表复制到当前工作簿的第一个工作表前 End Sub 这样是最简单的代码了&#xff0c;但是有些限制…

Android仿ios二级菜单侧滑,仿IOS的列表项滑动菜单——ListItemMenu

一个简单的仿IOS的列表项滑动菜单(也不知道怎么描述比较好)。顺手做出来的小东西&#xff0c;就分享给大家了。仿iOS列表项滑动菜单:1、滑动出现菜单&#xff0c;越界阻尼效果&#xff1b;2、删除列表项效果。GitHub地址:https://github.com/zarics/ListItemMenu1.[代码]布局示…

[html] 你认为一个好的布局应该是什么样的?有哪些需要注意的地方?

[html] 你认为一个好的布局应该是什么样的&#xff1f;有哪些需要注意的地方&#xff1f; 先布局整体,再细分到模块; 先抽离组件再分离业务个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢迎大家一起讨论 主目录 与…

判断cloudblob是否存在

这是开博的第一篇&#xff0c;还要废话一下。我写的很多内容都是网上找资料然后自己总结出来的&#xff0c;原出处已经很难找到了&#xff0c;所以不会标出引用的内容。如果侵犯到您的版权&#xff0c;请和我联系&#xff0c;我会删改相关的内容。 cloudblob 是azure blob的一个…

android studio炸包怎么导入,请问android studio如何引入包

梦里花落0921jar包放项目根目录libs文件夹右键选择Add As Library"剩选项默认行点击。Show import popup&#xff0c;这个是用于编辑XML时&#xff0c;自动会弹出一个import的对话框&#xff0c;问你是否需要导入。JavaInsert imports on paste:(All Ask None),这个其实就…

[html] button标签的type默认值是什么呢?

[html] button标签的type默认值是什么呢&#xff1f; Internet Explorer 的默认类型是 "button"&#xff0c;而其他浏览器中&#xff08;包括 W3C 规范&#xff09;的默认值是 "submit"。个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放…

Java 理论与实践:让 J2EE 脱离容器

大多数项目不是属于 J可以存在于 J2EE 容器之外Goetz 分析如何在 J2SE 应他读者分享您关于本文的心2EE 应用程序就是属于 J2SE 应&#xff0c;并且有些 J2SE 应用程序可以用程序中使用某些 J2EE 服务。得。&#xff08;您也可以单击文章顶部或用程序。不过&#xff0c;有一些 J…

eclipse IDE中無法打開android模擬器

转帖&#xff1a;http://blog.csdn.net/wang_shaner/article/details/6784852 错误提示为&#xff1a; invalid command-line parameter: Files\Android\android-sdk\tools/emulator-arm.exe.Hint: use foo to launch a virtual device named foo.please use -help for more in…