Java Stack

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> stack = new Stack<>(); // menambah elemen pada stack stack.push("Adi"); stack.push("Budi"); stack.push("Cindy"); // tampilkan elemen dari tumpukan teratas for(String s: stack) { System.out.println( stack.get((stack.size() - 1) - stack.indexOf(s)) ); } // melihat elemen teratas System.out.println("Elemen teratas: "+ stack.peek()); // menghapus elemen System.out.println("Hapus elemen : "+ stack.pop()); // cek apakah stack kosong? System.out.println("Stack kosong? : "+ stack.empty()); System.out.println("Hapus elemen : "+ stack.pop()); System.out.println("Hapus elemen : "+ stack.pop()); // cek apakah stack kosong? System.out.println("Stack kosong? : "+ stack.empty()); } }
Editor Settings
Theme
Key bindings
Full width
Lines