Let’s look now at a few methods for extracting keys and values. To find out the index of a value in the SortedList, use the IndexOfValue method, which accepts an object as an argument. If the object exists in the collection, it returns its index; if not, it returns the value −1. If the same value appears more than once in the collection, the IndexOfValue property will return the first instance of the value. Notice that the IndexOfValue property performs a case-sensitive search when applied to strings. The following statement will return the index 6 (the item you’re looking for is in the seventh place in the original SortedList):
Debug.WriteLine(sList.IndexOfValue("item 7"))
Code language: CSS (css)
You can also find out the index of a specific key, with the IndexOfKey method, whose syntax is similar. Instead of a value, it locates a key. The following statement will return the index 3 (the key you’re looking for is in the fourth place in the SortedList):
Debug.WriteLine(sList.IndexOfKey(211))
Code language: CSS (css)
If either the key or the value you’re searching for can’t be found, the IndexOfKey and IndexOfValue methods will return −1.
The GetKey and GetValue methods allow you to retrieve the index that corresponds to a specific key or value in the SortedList. Both methods accept an object as an argument and return an index.
Finally, you can combine the two methods to retrieve the key that corresponds to a value with a statement like the following one:
Debug.WriteLine( _
sList.GetKey(sList.IndexOfValue("item 7")))
Code language: CSS (css)
This statement will print the value 12, based on the contents of the sList collection in Listing 14.9.
You can retrieve the keys in a SortedList collection and create another list by using the GetKeyList method. Likewise, the GetValueList method returns all the values in the SortedList. The following code extracts the keys from the sList SortedList and stores them in the keys list. Then, it scans the list with the help of the key variable and prints all the keys:
Dim keys As IList
keys = slist.GetKeyList()
Dim key As Integer
For Each key In Keys
Debug.WriteLine(key)
Next
Code language: PHP (php)
You can also extract both the keys and the values from a SortedList and store them in an ArrayList, as shown here:
Dim AllKeys As New ArrayList()
AllKeys.InsertRange(0, sList.GetValueList)
Code language: CSS (css)
Each item is stored at a specific location in the SortedList, and you can find the location of each item via a loop like the following:
Dim idx As Integer
For idx = 0 To sList.Count - 1
Debug.WriteLine("ITEM: " & sList.GetByIndex(idx).ToString & _
" is at location " & idx.Tostring)
Code language: PHP (php)
The partial output produced by this code segment is this:
ITEM: item 9 is at location 0
ITEM: item 4 is at location 1
ITEM: item 3 is at location 2
ITEM: item 8 is at location 3
Code language: HTTP (http)
You can also find the location of each key by using a loop like the following:
For idx = 0 To sList.Count - 1
Debug.WriteLine(”The key at location ” & idx.ToString & ” is ” & _
sList.GetKey(idx).ToString)
Next
The partial output produced by the preceding code segment is as follows:
The key at location 0 is 110
The key at location 1 is 115
The key at location 2 is 116
The key at location 3 is 211
Notice that the keys are rearranged as they’re added to the list, and they’re always physically sorted; you can’t assume that an element’s position remains the same in the course of the application.
Remember the WordFrequencies project we built earlier to demonstrate the use of the HashTable class? Change the declaration of the WordFrequencies variable from HashTable to SortedList, and the project will work as before. The only difference is that the words will appear in the RichTextBox control sorted alphabetically when you click the Show Word Count button.
Other collections
The System.Collections class exposes a few more collections, including the Queue and the Stack collections. The main characteristic of these two collections is how you add and remove items to them. When you add items to a Queue collection, the items are appended to the collection. When you remove items, they’re removed from the top of the collection. Queues are known as last in, first out (LIFO) structures because you can extract only the oldest item in the queue. You’d use this collection to simulate the customer line in a bank or a production line.
The Stack collection inserts new items at the top, and you can remove only the top item. The Stack collection is a first in, first out (FIFO) structure. You’d use this collection to emulate the stack maintained by the CPU, one of the most crucial structures for the operating system and applications alike. Stack and Queue collections are used heavily in computer science but hardly ever in business applications.