本文共 3688 字,大约阅读时间需要 12 分钟。
Stack class is available in java.util package.
堆栈类在java.util包中可用。
The Stack Class is child class of Vector class.
堆栈类是向量类的子类。
As we know that Stack is LIFO (Last-In-First-Out) data structure.
众所周知,Stack是LIFO(后进先出)数据结构。
Here the above point given LIFO means last inserted element will be popped first.
此处给出LIFO的上述点意味着最后插入的元素将首先弹出。
Stack class contains only one constructor i.e. Default Constructor.
堆栈类仅包含一个构造函数,即默认构造函数。
The syntax of default constructor is given below:
默认构造函数的语法如下:
Stack st = new Stack();
Stack class defines various methods to perform various task onto the stack. The names of the stack class methods is given below:
堆栈类定义了各种方法来执行各种任务到堆栈上。 堆栈类方法的名称如下:
Now, we will see the purpose of the Stack methods given above and we will study one by one.
现在,我们将看到上面给出的Stack方法的目的,并且我们将一一研究。
i) void push(Object obj)
i)void push(Object obj)
This method is used to push an element into the stack.
此方法用于将元素压入堆栈。
This method takes one parameter in the method of the Stack and the parameter is the object is to be inserted into the Stack.
此方法在Stack方法中使用一个参数,该参数是要插入到Stack中的对象。
ii) Object pop()
ii)对象pop()
This method is used to remove an element from the Stack and then it returns to the top of the Stack.
此方法用于从堆栈中删除元素,然后返回到堆栈顶部。
This method does not pass any parameter in the method of the Stack.
此方法在Stack方法中不传递任何参数。
iii) boolean empty()
iii)布尔型empty()
This method is used to check whether the Stack is empty or not.
此方法用于检查堆栈是否为空。
We don't pass any object as a parameter in the method of the Stack.
在Stack方法中,我们不传递任何对象作为参数。
The return type of this method is Boolean so it returns true if Stack is empty.
此方法的返回类型为Boolean,因此如果Stack为空,则返回true。
iv) Object peek()
iv)对象peek()
This method is used to return top of the Stack.
此方法用于返回堆栈的顶部。
We don't pass any object as a parameter in the method of the Stack.
在Stack方法中,我们不传递任何对象作为参数。
v) int search(Object obj)
v)int搜索(Object obj)
This method is used to search an element into the stack.
此方法用于将元素搜索到堆栈中。
We pass only one object as a parameter in the method of the Stack and the parameter is the object is to be searched into the Stack.
我们仅将一个对象作为参数传递给Stack方法,而该参数是要在Stack中搜索的对象。
The return type of this method is int so if an object is found in Stack so it returns offset or address from the top of the Stack else it returns -1 if an object is not found onto the Stack.
此方法的返回类型为int,因此,如果在堆栈中找到一个对象,则它从堆栈顶部返回偏移量或地址,否则,如果在堆栈上未找到对象,则返回-1。
Example:
例:
// Java program to demonstrate the behavior of the Stack.import java.util.*;class StackClass { public static void main(String[] args) { // Creating an instance of Stack Stack st = new Stack(); // By using push() method to push few elements onto the Stack st.push(10); st.push(20); st.push(30); st.push(40); st.push(50); // Display Current Stack System.out.println("Current Stack List:" + st); // By using pop() method to remove an element from the // stack and return top of the Stack System.out.println("The Popped Element:" + st.pop()); // By using peek() method to return top of the Stack System.out.println("The top of the Stack:" + st.peek()); // By using search() method to search an element from the Stack System.out.println("Search element is:" + st.search(30)); System.out.println("Search element not found:" + st.search(60)); }}
Output
输出量
E:\Programs>javac StackClass.javaE:\Programs>java StackClassCurrent Stack List:[10, 20, 30, 40, 50]The Popped Element:50The top of the Stack:40Search element is:2Search element not found:-1
翻译自:
转载地址:http://upvzd.baihongyu.com/