Stack Java Help


Stack is a subclass of Vector that implements, a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector, and adds several of its own, shown in Table 15-11. To put an object on the top of the stack, call push( I, To remove and return the top element, call pope ); An Empty Stack  is thrown if you call pope ) when the invoking stack is empty. You can use peek( ) to return, but not remove, the top object. The empty( ) method returns true i( nothing is on the stack, The search ) method determines whether an object exists on the stack, and returns the number of pops that are required to bring it to the top of the stack. Here is an example that creates a stack, pushes several Integer objects onto it, and then pops them off again: The following is the output produced by the program; notice how the exception handler for Empty Stack Exception is caught so that you can gracefully handle a stack underflow: ” stack: ( )

stack: (42]
stack: (42, 66)
stack: [42, 66, 99]
pop -> 99
stack: (42, 66)
pop -> 66
stack: (42)
pop -> 42
stack: [ }
pop -> empty s’t ask


Dictionary is an abstract class that represents a key/value storage repository and operates much like Map. Given a key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key. Thus, like a map, a’ dictionary can be thought of as a list of key /value pairs. Although not actually deprecated by Java 2, Dictionary is classified as obsolete, because it is superseded by Map. However, Dictionary is currently in widespread use and thus is fully discussed here. . The abstract methods defined by Dictionary are listed. To add a key and a value, use the put( ) method. Use get( ) to retrieve the value of a given key. The keys and values can each be returned .as an Enumeration by the keys() and elements( ) methods, respectively. The size ) method returns the number of key /value pairs stored in a dictionary, and is Empty( ) returns true when the dictionary is empty. You can use the remove() method to delete a key/value pair.

  • Feel free to send us an inquiry, we will reply back in hours.


Posted on September 17, 2014 in java util Part 1 The Collections Framework

Share the Story

About the Author

Back to Top