[ Pobierz całość w formacie PDF ]
.ContainsKey(key) ThenhTable.Add(key, value)End IfThe Values and Keys properties allow you to retrieve all the values and the keys in theHashTable.Both properties are collections and expose the usual members of a collection.To iteratethrough the values stored in the HashTable hTable, use the following loop:Dim itm As ObjectFor Each itm In hTable.ValuesConsole.WriteLine(itm)NextThere is only one method to remove items from an ArrayList: the Remove method, whichaccepts as argument the key of the item to be removed:hTable.Remove(key)To extract items from a HashTable, use the CopyTo method.This method copies the items to aone-dimensional array, and its syntax isnewArray = HTable.CopyTo(arrayName)www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA 2877c11.qxd 11/11/01 4:18 PM Page 499THE HASHTABLE COLLECTION 499You must set up the array that will accept the items beforehand, because this method can throwseveral different exceptions for various error conditions.The array that accepts the values must beone-dimensional, and there should be enough space in the array for the HashTable s values.More-over, the array s type must be Object, because this is the type of the items you can store in aHashTable.Listing 11.7 demonstrates how to scan the keys of a HashTable through the Keys property andthen use these keys to access the items through the Item property (and passing the key as argument).Listing 11.7: Iterating a HashTablePrivate Function ShowHashTableContents(ByVal table As Hashtable) As StringDim msg As StringDim element, key As Objectmsg =  The HashTable contains  & table.Count.tostring &  elements: & vbCrLfFor Each key In table.keyselement = table.Item(key)msg = msg & vbCrLfmsg = msg &  Element Type =  & element.GetType.ToString & vbCrLfmsg = msg &  Element Key=  & Key.ToStringmsg = msg &  Element Value=  & element.ToString & vbCrLfNextReturn(msg)End SubTo print the contents of a HashTable variable on the Output window, call the ShowHashTable-Contents() function, passing the name of the HashTable as argument, and then print the stringreturned by the function:Dim HT As New HashTable{ statements to populate HashTable }Console.WriteLine(ShowHashTableContents(HT))VB.NET at Work: The WordFrequencies ProjectIn this section, you ll develop an application that counts word frequencies in a text.The Word-Frequencies application scans text files and counts the occurrences of each word in the text.As youwill see, the HashTable is the natural choice for storing this information, because you want to accessa word s frequency by the word.To retrieve (or update) the frequency of the word elaborate, forexample, you will use the expression:Words( ELABORATE ).ValueArrays and ArrayLists are out of the question, because they can t be accessed by a key.You could alsouse the SortedList collection, which is described later in this chapter, but this collection maintains itsitems sorted at all times.If you need this functionality as well, you can modify the application accord-ingly.The items in a SortedList are also accessed by keys, so you won t have to introduce substantialchanges in the code.www.sybex.comCopyright ©2002 SYBEX, Inc., Alameda, CA 2877c11.qxd 11/11/01 4:18 PM Page 500500 Chapter 11 STORING DATA IN COLLECTIONSLet me start with a few remarks.First, all words we locate in the various text files will be con-verted to uppercase.Because the keys of the HashTable are case-sensitive, converting them to upper-case makes them unique.This way, we don t risk counting the same word in different cases as two ormore different words.The frequencies of the words can t be calculated instantly, because we need to know the totalnumber of words in the text.Instead, each value in the HashTable is the number of occurrences of aspecific word.To calculate the actual frequency of the same word, you must divide this value by thenumber of occurrences of all words, but this can happen only after we have scanned the entire textfile and counted the occurrences of each word.Since this operation will introduce delays in theapplication, I ve decided to keep track of number of occurrences only and calculate the word fre-quencies when requested.When the code runs into another instance of the word elaborate, it simply increases the matchingitem of the HashTable by one:Words( ELABORATE ).Value = Words( ELABORATE ).Value + 1The application s interface is shown in Figure 11.3.To scan another text file and process itswords, click the Read Text File button [ Pobierz caÅ‚ość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • agnieszka90.opx.pl