Java stack class usage instance (usage method of stack in Java)

Java, using Java util. Create objects using the constructor of the stack class.

public class Stack extends vector

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

Methods: 1 Public push (item) pushes an item to the top of the stack. It works the same as addelement (item).

The parameter item is pushed to the top of the stack. Return: item parameter;

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

Return: stack top object (the last item in the vector object).

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

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

Return: stack top object (the last item in the vector object).

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

4. Public Boolean empty (test whether the stack is empty.) return true if and only if there is no item in the stack, otherwise return false

5. Public int search (object o) returns the position of the object in the stack, with 1 as the cardinal number. If object o is an item in the stack, this method returns the distance from the nearest occurrence position to the top of the stack; The distance of the uppermost item in the stack is 1. Use the equals method to compare o with items in the stack...

Parameter: O target object;

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>