Google News
logo
Java Stack
Stack
stack is the subclass of Vector which is dynamic in nature and its size goes on increasing as the elements are added to it. Stack class allows creating the pile of the elements where, last element added to the stack is taken out first. This is also referred to as Last in First Out (LIFO).

For a Stack class, elements are pushed onto the stack by calling push() method and elements are retrieved using pop() method. First element of the stack is indicated by calling peek() method, which only points the element at the top of the stack but does not remove it. The search() method determines the existence of an object on the stack and to get the specified element to the top it performs those many pop calls.
Stack program
class Test 
{ 
public static void main(String[] args) 
{ 
Stack s=new Stack(); 
s.push("A"); 
s.push(10); 
s.push("aaa"); 
System.out.println(s); 
s.pop(); 
System.out.println(s); 
System.out.println(s.search("A")); 
} 
}
Output :
[A, 10, aaa]
[A, 10]
2