• Java 数据结构教程

清空栈中的元素



Stack 类的 clear() 方法用于清空当前栈的内容。

示例

import java.util.Stack;

public class ClearingElements {
   public static void main(String[] args) {
      Stack stack = new Stack();
      stack.push(455);
      stack.push(555);
      stack.push(655);
      stack.push(755);
      stack.push(855);
      stack.push(955);
      System.out.println("Contents of the stack :"+stack);
      stack.clear();
      System.out.println("Contents of the stack after clearing the elements :"+stack);
   }
}

输出

Contents of the stack :[455, 555, 655, 755, 855, 955]
Contents of the stack after clearing the elements :[]
广告