Java Stack class usages (how to use Stack in Java) _java

Source: Internet
Author: User

In JAVA, you create an object using the constructor method of the Java.util.Stack class.

public class Stack extends vector

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

Methods: 1. The public push (item) presses the item onto the top of the stack. The effect is the same as addelement (item).

Item that the parameter item presses onto the top of the stack. return: item parameter;

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

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

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

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

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

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

4. Public Boolean empty (whether the test stack is empty.) Returns true if and only if the stack does not contain any items, or false.

5. public int Search (object o) returns the position of the object in the stack to 1, and if 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, and the top item in the stack has a distance of 1. Use the Equals method to compare O with the items in the stack ...

Parameters: O target object;

Copy Code code as follows:

/**
* @author Yuanli
*/
Package Thinkingjava;
Import java.util.*;

Import Com.sun.org.apache.bcel.internal.generic.NEW;
/**
*
*/
public class Stacktest {

/**
* @param args
*/
public static void Main (string[] args) {
Stack stack = new stack (); Creating a Stack Object
System.out.println ("11111, Absdder, 29999.3 three elements into the stack");
Stack.push (New Integer (11111)); Press the integer 11111 into the stack
Printstack (stack); Show all the elements in the stack

Stack.push ("Absdder"); Push into the stack
Printstack (stack); Show all the elements in the stack

Stack.push (New Double (29999.3)); Push into the stack
Printstack (stack); Show all the elements in the stack

string s = new String ("Absdder");
SYSTEM.OUT.PRINTLN ("The element absdder position on the stack" +stack.search (s));
System.out.println ("element 11111 in the position of the stack" +stack.search (11111));

System.out.println ("11111, Absdder, 29999.3 three elements out of the stack"); Pop-up stack top element
System.out.println ("element" +stack.pop () + "out stack");
Printstack (stack); Show all the elements in the stack
System.out.println ("element" +stack.pop () + "out stack");
Printstack (stack); Show all the elements in the stack
System.out.println ("element" +stack.pop () + "out stack");
Printstack (stack); Show all the elements in the stack


}

private static void Printstack (Stack<integer> Stack) {
if (Stack.empty ())
System.out.println ("Stack is empty, no elements");
else {
System.out.print ("Elements in Stack:");
Enumeration items = Stack.elements (); Get the enumeration object in stack
while (Items.hasmoreelements ())//display all elements in an enumeration (stack)
System.out.print (items.nextelement () + "");
}
System.out.println (); Line Wrap
}
}

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.