Monday, December 1, 2014

STACK in JAVA

STACK is a special collection of objects such as QUEUE but LIFO approach. STACK class diagram for the objects can be viewed on this website. to use this object, we have to declare the package:
Import java.util. *;

STACK object initialization as follows:

Stack stack = new Stack ();
How to insert data into a Stack object as follows:
Stack.push (2);
Stack.push (10);
Stack.push (1);
Above we add a numeric object.

To access the data we can use the method firselement (), pop (), and iterators. if we use the method firtElement then we will obtain the first data of the object stack. whereas if we call the pop (); then the data will be missing from the collection. This beikut illustration of the program:

// Take the first data
Objects firstElement = stack.firstElement ();
System.out.println ("first element:" + firstElement);
// Retrieve data based index
Objects dt = stack.get ();
System.out.println ("data:" + dt);

Iterator iterator = stack.iterator ();
While (iterator.hasnext ()) {
Integer items = (Integer) iterator.next ();
System.out.println ("Data iterators:" + item);
}
// Retrieving the latest data entered
Object ret = stack.pop ();
System.out.println ("Data pop:" + ret);
Save the above program code da run. then if successful will produce according to the picture below:
STACK JAVA terpendek
JAVA STACK
Whereas, if we want to remove the data from STACK object by using the method remove () with the parameter index data or a data object d delete, Ajuga kit can delete all the data by calling the method removeAllElement (). The following illustration of the program:
// Delete the data by index

Objects objRemoved = stack.remove (0);

System.out.println ("object index 0 are in the clear:" + objREmoved);
// Delete the data based on the data
Boolean removed = stack.remove ((Object) 10);
System.out.println ("object 10 are deleted:" + removed);
// Remove all objects
Stack.removeAllElements ();
The results of execution of the above program code is as follows:
JAVA STACK keren
JAVA STACK


Earlier material about SET , MAP and QUEUE in JAVA  
Earlier material about  COLLECTION in JAVA

No comments:

Post a Comment