JAVA collection - Stack

Stack stack is only one no-argument constructor, and ArrayList, LinkedLink, Voctor collection usage is almost, but there are several functions Stack collection is ArrayList, LinkedLink, Vector does not have.

  .push (num) // stack

  .pop () // pop the top element

  .empty () // determine whether the stack is empty

  .peek () // get the top element

  .search (num) // num determination whether the end of the element in the stack, if a return, not -1.

 

1  / * 
2  * Author: white passenger C
 3  * Time: 29 February 2020
 4  * Content: Stack Usage
 5   * / 
6  
7  Package Penalty for com.beekc.www;
 8  Import Classes in java.util *. ;
 9  
10  public  class Beekc {
 . 11      public  static  void main (String [] args) throws Exception {
 12 is          // instantiate a stack 
13 is          stack stack = new new stack ();
 14  
15          // instantiate an employee object 
16          Emp = emp1 new newEmp ( "000", "off white C", 1000 );
 . 17          EMP EMP2 = new new EMP ( "001", "white", 1200 );
 18 is  
. 19          // stack, the first data 
20 is          stack.push ( emp1);
 21 is          // stack, the second data 
22 is          stack.push (EMP2);
 23 is          // the stack, the second first-out data 
24          stack.pop ();
 25  
26 is          for ( int I = 0; I <stack.size (); I ++ )
 27          {
 28              . of System.out.print (((Emp) stack.get (I)) getEmpNo () + "\ T" );
 29             Of System.out.print (((Emp) stack.get (I)) getName () + "\ T." );
 30              . System.out.println (((Emp) stack.get (I)) getSal () );
 31          }
 32  
33 is  
34 is      }
 35  }
 36  
37 [  // employee class 
38 is  class Emp {
 39      // Science No. 
40      Private String the empNo;
 41 is      // name 
42 is      Private String name;
 43 is      // salary 
44 is      Private  a float SAL;
 45  
46 is      // constructor 
47      public Emp(String empNo, String name, float sal)
48     {
49         this.empNo = empNo;
50         this.name = name;
51         this.sal = sal;
52     }
53 
54     public String getEmpNo() {
55         return empNo;
56     }
57 
58     public void setEmpNo(String empNo) {
59         this.empNo = empNo;
60     }
61 
62     public String getName() {
63         return name;
64     }
65 
66     public void setName(String name) {
67         this.name = name;
68     }
69 
70     public float getSal() {
71         return sal;
72     }
73 
74     public void setSal(float sal) {
75         this.sal = sal;
76     }
77 }

 

operation result

Guess you like

Origin www.cnblogs.com/beekc/p/12383262.html