Java Collection: Stack - BunksAllowed

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

Community

demo-image

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 + "]";
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

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