清空棧中的元素
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 :[]
廣告