Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  302 随笔 :: 26 文章 :: 82 评论 :: 0 Trackbacks
Class Stack<E>
						java.lang.Object
						extended by 
						java.util.AbstractCollection<E>
      extended by java.util.AbstractList<E>
          extended by java.util.Vector<E>
              extended by java.util.Stack<E>
 
Method Summary
 boolean empty ()
          Tests if this stack is empty.
  E peek ()
          Looks at the object at the top of this stack without removing it from the stack.
  E pop ()
          Removes the object at the top of this stack and returns that object as the value of this function.
  E push ( E  item)
          Pushes an item onto the top of this stack.
 int search ( Object o)
          Returns the 1-based position where an object is on this stack.
 
 
 
另外还继承了java.util.Vector<E>中的基本方法,常见的用法如下所示:
 
 
import java.util.*;
 
public class kkk{
 public static void main(String args[]){
  int m = 1;
  Stack<Integer> a = new Stack<Integer>();
  
  a.push(m);   //压入1
  a.push(m+1); //压入2
  a.push(m+2); //压入3
  a.push(m+3); //压入4
  a.push(m+4); //压入5
    System.out.println(a.push(m+5)); //压入6,并返回当前值
 
    System.out.println(a.empty());   //判断栈是否为空,为空则TRUE
    System.out.println(a.isEmpty()); //判断栈是否为空,为空则TRUE
   
  System.out.println(a.size());     //返回当前栈长度
  System.out.println(a.toString()); //返回当前栈的内容
  
  System.out.println(a.lastElement());  //返回当前栈中的最后一个元素6
  System.out.println(a.firstElement()); //返回当前栈中的第一个元素1
  
  System.out.println(a.peek()); //返回当前值6
  System.out.println(a.pop());  //返回当前值并压出6
  System.out.println(a.pop());  //返回当前值并压出5
  
  System.out.println(a.search(3));
  //搜索“3”在栈中出现的位置,顶端为1,其余依次累加,若有多个则返回第1个的位置
 
  a.add(m+9); //压入10,跟push一样的效果
  a.clear(); //清空栈
  
    System.out.println(a.empty());   //判断栈是否为空,为空则TRUE
    System.out.println(a.isEmpty()); //判断栈是否为空,为空则TRUE 
 
 }
}
 
 
 




-The End-

posted on 2008-12-01 22:42 decode360-3 阅读(325) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: