Java Collection: Stack - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community


The Stack class is implemented based on the concept of the stack in Data Structure.


Hierarchy of Stack class


It inherits the Vector class, which in turn inherits AbstractList class.

Source code of Item.java
package com.t4b.test; public class Item { String name; int id; double price; public Item(String name, int id, double price) { super(); this.name = name; this.id = id; this.price = price; } @Override public String toString() { return "Item [name=" + name + ", id=" + id + ", price=" + price + "]"; } }

Source code of TestMain.java
package com.t4b.test; import java.util.Stack; public class TestMain { public static void main(String[] args) { Stack<Item> items = new Stack<Item>(); items.push(new Item("Apple", 1, 150.0)); items.push(new Item("Grape", 2, 250.0)); items.push(new Item("Mango", 3, 10)); for (Item i : items) System.out.println(i); Item itm = items.pop(); for (Item i : items) System.out.println(i); System.out.println(items.size()); } }

Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.