"A detailed study of the Java Collection 9" How to use Java stacks (stack)

Source: Internet
Author: User

Stack is a common data structure, the stack only allows access to the top of the stack of elements, the stack is like a cup, each time can only take the top of the cup of things, and for the stack can only access its top of the stack element each time,
This allows other elements below the top element of the protection stack to be reached. "Advanced post-out" or "LIFO" is a big feature of the stack,
The elements of the advanced stack are always waiting until the elements of the backward stack are out of the stack. Recursion is the use of the system stack, temporarily save temporary results, to protect the temporary results.

A stack is a reference to a variable data and object that holds a primitive type, but the object itself is not stored in the stack, but is stored in the heap (the new object) or in a constant pool (the string constant object is stored in a constant pool. )。

objects in stacks and constant pools can be shared, and objects in the heap cannot be shared. The data size and life cycle in the stack can be determined, and the data disappears when no references point to the data. The garbage collector is responsible for the collection of objects in the heap, so the size and life cycle do not need to be determined. The data for local variables exists in the stack memory. The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handle (reference).

A stack is a "last-in, first-out" (LIFO) data structure that can only be inserted at one end (called a "stack") or deleted (called "Out-of-stack") data.

In JAVA, objects are created using the Java.util.Stack class's construction method.

public class Stack extends vector

Construction method: Public stack() creates an empty stack.

Method: 1. Public push (item) presses the item into the top of the stack. The effect is the same as addelement (item).

The item that the parameter item presses into the top of the stack. return: item parameter;

2. Public pop () removes the top of the stack object and returns the object as the function's value.

Returns: The top object of the stack (the last item in the Vector object).

Throws an exception: Emptystackexception if the stack is empty ...

3. Public peek () view the top of the stack object without removing it ...

Returns: The top object of the stack (the last item in the Vector object).

Throws an exception: Emptystackexception if the stack is empty ...

4. Public boolean empty (test stack is empty.) Returns False if and only if no items are included in the stack.

5. public int Search (object o) returns the position of the object in the stack, base 1, if the object o is an item in the stack, the method returns the distance from the top of the stack to the top of the stack; the distance from the topmost item in the stack

//Stacks, sub-classes of vectors Public classStackdemo {//put elements into the top of the stack    Static voidShowpush (Stack St,inta) {St.push (NewInteger (a)); System.out.println ("Push (" + A + ")"); System.out.println ("Stack:" +St); }    //Delete an element from the top of the stack    Static voidShowpop (Stack St) {System.out.print ("Pop"); //determine if the stack is empty        if(St.empty ()) {System.out.println ("Stack is empty."); } Else{Integer a=(Integer) St.pop ();            System.out.println (a); System.out.println ("Stack:" +St); }    }    //view top of stack elements    Static voidShowpeek (Stack St) {System.out.print ("Peek-in"); if(St.empty ()) {System.out.println ("Stack is empty."); } Else{Integer a=(Integer) St.peek ();            System.out.println (a); System.out.println ("Stack:" +St); }    }    //querying the specified element    Static voidShowsearch (Stack St,inti) {System.out.print ("Search" +i); Integer Index=(Integer) st.search (i); System.out.println ("--index" +index); System.out.println ("Stack:" +St); }     Public Static voidMain (String args[]) {Stack St=NewStack (); System.out.println ("Stack:" +St); Showpush (St,42); Showpush (St,66);        Showpeek (ST); Showsearch (St,66); Showsearch (St,88);        Showpop (ST);        Showpop (ST);    Showpop (ST); }}

Operation Result:

Stack: []
Push (42)
Stack: [42]
Push (66)
Stack: [42, 66]
66, Peek
Stack: [42, 66]
1, 66--index, search
Stack: [42, 66]
Search-88--index-1
Stack: [42, 66]
Pop--66
Stack: [42]
Pop--42
Stack: []
POPs, Stack is empty.

"A detailed study of the Java Collection 9" How to use Java stacks (stack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.