Java 中 pop() 方法有什麼作用?


pop() 方法用於移除此棧頂部的物件,並返回該物件作為此函式的值。

示例

import java.util.*;

public class StackDemo {
   public static void main(String args[]) {
      Stack st = new Stack();
      st.push("Java");
      st.push("Source");
      st.push("code");
      System.out.println("Removed object is: "+st.pop());
      System.out.println("Elements after remove: "+st);
   }
}

輸出

Removed object is: code
Elements after remove: [Java, Source]

更新時間: 2019 年 7 月 30 日

463 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.