1160 Java Utilities Package and Bit Manipulation Chapter (Web host server)

1160 Java Utilities Package and Bit Manipulation Chapter 20 180 } // end class StackTest Fig. 20.2 Demonstrating class Stackof package java.util(part 5 of 5). Line 25 creates an empty Stack. Line 43 calls Stackmethod push to add its argument to the top of the stack. The method returns an Objectreference to its argument. Line 61 calls Stackmethod pop to remove the top element of the stack. The method returns an Object reference to the removed element. If there are no elements in the Stack, method pop throws an EmptyStackException. Line 85 calls Stack method peek to view the top element of the stack without removing the element. Method peekreturns an Objectreference to the element. Line 108 calls Stackmethod empty to determine whether the stack is empty. If it is empty, the method returns true; otherwise, the method returns false. Line 127 calls Stack method search to determine whether its argument is in the stack. If so, the method returns the position of the element in the stack. Note that the top element is position 1. If the element is not in the stack, 1is returned. The entire publicinterface of class Vectoris actually part of class Stack, because Stackinherits from Vector. To prove this, our example provides a button to display the contents of the stack. This button invokes method elementsto get an Enumerationof the stack; it then uses the Enumerationto walk through the stack elements. Testing and Debugging Tip 20.1 Stack extends Vector, so the user may perform operations on Stack objects that are ordinarily not allowed on conventional stack data structures. This could “corrupt” the elements of the Stack and destroy the integrity of the Stack. 20.4 DictionaryClass A Dictionary maps keys to values. When searching a Dictionaryfor a value, the program provides a key and the Dictionaryreturns the corresponding value. Dictionary is an abstractclass. In particular, it is the superclass of class Hashtable, which we discuss in Section 20.5. Class Dictionaryprovides the publicinterface methods required to maintain a table of key value pairs where the keys help store and retrieve the values in the table. Each key in the table is unique. The data structure is similar to a dictionary of words and definitions the word is the key that is used to look up the definition (i.e., the value). Dictionary method size returns the number of key value pairs in a Dictionary object. Method isEmpty returns true if a Dictionary is empty, and false otherwise. Method keys returns an Enumeration that a program can use to iterate through a Dictionary s keys. Method elements returns an Enumeration that a program can use to iterate through a Dictionary s values. Method get returns the object that corresponds to a given key value. Method put puts an object associated with a given key into the table. Method remove removes an element corresponding to a given key and returns a reference to it.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

Leave a Reply