堆栈如何在java中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
java堆栈中类继承于向量,其特性为后进先出(lastinfirstout)。
入栈和出栈实例图:
package com.lanhuigu.java.ListTest;
import java.util.Stack;
public class  StackTest {
public static  void main (String [], args), {
Stack ();//,入栈顺序:,a, b, c, d, e
staffs.push (“a");
staffs.push (“b");
staffs.push (“c");
staffs.push (“d");
staffs.push (“e");//,出栈顺序:,e、d、c、b, a
而(,! staffs.isEmpty ()), {
System.out.print (staffs.pop (), +,“,“);
}
}
} 程序运行结果:
edcba
堆栈类中方法:
官网API:
package com.lanhuigu.java.ListTest;
import java.util.Stack;
public class  StackMethodTest {
public static  void main (String [], args), {
Stack ();//,入栈顺序:,a, b, c, d, e
staffs.push (“a");
staffs.push (“b");
staffs.push (“c");
staffs.push (“d");
staffs.push (“e");
System.out.println(“空():“,+,staffs.empty ());
System.out.println (“peek ():“, +, staffs.peek ());
System.out.println(“搜索(Object o):“, +, staffs.search (“a"));
System.out.println(“搜索(Object o):“, +, staffs.search (“e"));
System.out.println(“搜索(Object o):“, +, staffs.search (“no"));//,出栈顺序:,e、d、c、b, a
而(,! staffs.isEmpty ()), {
System.out.print (staffs.pop (), +,“,“);
}
System.out.println(“=====空栈中使用方法=======?;
System.out.println(“空():“,+,staffs.empty ());//System.out.println (“peek ():“, +, staffs.peek());//,在空栈中使用时报错,因为没有栈顶元素,
System.out.println(“搜索(Object o):“, +, staffs.search (“a"));
System.out.println(“搜索(Object o):“, +, staffs.search (“e"));
System.out.println(“搜索(Object o):“, +, staffs.search (“no"));//System.out.print (staffs.pop());//,空栈中移除栈顶元素,报的错
}
} 程序运行结果:
关于堆栈如何在java中使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。