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 dataObjects 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:
// 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
Earlier material about SET , MAP and QUEUE in JAVA
Earlier material about COLLECTION in JAVA


No comments:
Post a Comment