📄 javautil.doc10.html
字号:
<html>
<head>
<title>The Java Language Specification The Package java.util</title>
</head>
<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>
<a href="index.html">Contents</a> | <a href="javautil.doc9.html">Prev</a> | <a href="javautil.doc11.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<hr><br>
<a name="24949"></a>
<center><h1>21.12 The Class <code>java.util.Stack</code></h1></center>
<a name="23026"></a>
The class <code>Stack</code> extends <code>Vector</code> with five operations that allow a vector to be
treated as a stack. The usual <code>push</code> and <code>pop</code> operations are provided, as well as a
method to <code>peek</code> at the top item on the stack, a method to test for whether the stack
is <code>empty</code>, and a method to <code>search</code> the stack for an item and discover how far it is
from the top.
<p><pre><a name="23027"></a>public class <code><b>Stack</b></code> extends Vector {
<a name="23028"></a> public Object <code><b>push</b></code>(Object item);
<a name="23029"></a> public Object <code><b>pop</b></code>() throws EmptyStackException;
<a name="23030"></a> public Object <code><b>peek</b></code>() throws EmptyStackException;
<a name="23031"></a> public boolean <code><b>empty</b></code>();
<a name="23032"></a> public int <code><b>search</b></code>(Object o);
<a name="23033"></a>}
</pre><a name="23034"></a>
When a stack is first created, it contains no items.<p>
<a name="23035"></a>
<p><font size=+1><strong>21.12.1 </strong> <code>public Object <code><b>push</b></code>(Object item)</code></font>
<p>
<a name="23036"></a>
The <code>item</code> is pushed onto the top of this stack. This has exactly the same effect as:
<p><pre><a name="23037"></a>addElement(item)
</pre><a name="23041"></a>
See method <code>addElement</code> of <code>Vector</code> <a href="javautil.doc9.html#22408">(§21.11.13)</a>.<p>
<a name="23043"></a>
<p><font size=+1><strong>21.12.2 </strong> <code>public Object <code><b>pop</b></code>() throws EmptyStackException</code></font>
<p>
<a name="23044"></a>
If the stack is empty, an <code>EmptyStackException</code> is thrown. Otherwise, the topmost
item (last item of the <code>Vector</code>) is removed and returned.
<p><a name="23046"></a>
<p><font size=+1><strong>21.12.3 </strong> <code>public Object <code><b>peek</b></code>() throws EmptyStackException</code></font>
<p>
<a name="23047"></a>
If the stack is empty, an <code>EmptyStackException</code> is thrown. Otherwise, the topmost
item (last item of the <code>Vector</code>) is returned but not removed.
<p><a name="23048"></a>
<p><font size=+1><strong>21.12.4 </strong> <code>public boolean <code><b>empty</b></code>()</code></font>
<p>
<a name="23049"></a>
The result is <code>true</code> if and only if the stack contains no items.
<p><a name="23050"></a>
<p><font size=+1><strong>21.12.5 </strong> <code>public int <code><b>search</b></code>(Object o)</code></font>
<p>
<a name="26478"></a>
If the object <code>o</code> occurs as an item in this <code>Stack</code>, 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 <code>1</code>. The <code>equals</code> method
<a href="javalang.doc1.html#14865">(§20.1.3)</a> is used to compare <code>o</code> to the items in this <code>Stack</code>.<img src="javautil.doc.anc4.gif">
<p>
<hr>
<!-- This inserts footnotes--><p>
<a href="index.html">Contents</a> | <a href="javautil.doc9.html">Prev</a> | <a href="javautil.doc11.html">Next</a> | <a href="j.index.doc1.html">Index</a>
<p>
<font size=-1>Java Language Specification (HTML generated by Suzette Pelouch on February 24, 1998)<br>
<i><a href="jcopyright.doc.html">Copyright © 1996 Sun Microsystems, Inc.</a>
All rights reserved</i>
<br>
Please send any comments or corrections to <a href="mailto:doug.kramer@sun.com">doug.kramer@sun.com</a>
</font>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -