23.11.2017 Views

java_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Java<br />

First element: 1<br />

Last element: 12<br />

Vector contains 3.<br />

Elements in vector:<br />

1 2 3 4 5.45 6.08 7 9.4 10 11 12<br />

The Stack Class<br />

Stack is a subclass of Vector that implements a standard last-in, first-out stack.<br />

Stack only defines the default constructor, which creates an empty stack. Stack includes<br />

all the methods defined by Vector, and adds several of its own.<br />

Stack( )<br />

Apart from the methods inherited from its parent class Vector, Stack defines the following<br />

methods:<br />

Sr. No.<br />

1<br />

2<br />

3<br />

4<br />

boolean empty()<br />

Methods with Description<br />

Tests if this stack is empty. Returns true if the stack is empty, and returns<br />

false if the stack contains elements.<br />

Object peek( )<br />

Returns the element on the top of the stack, but does not remove it.<br />

Object pop( )<br />

Returns the element on the top of the stack, removing it in the process.<br />

Object push(Object element)<br />

Pushes the element onto the stack. The element is also returned.<br />

int search(Object element)<br />

5<br />

Searches for the element in the stack. If found, its offset from the top of the<br />

stack is returned. Otherwise, .1 is returned.<br />

428

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!