Figure 2.3
Output of Listing 2.1.3 when viewed through a browser.
Listing 2.1.3 begins with the instantiation of the SortedList class (line 4). slTestScores, the SortedList instance, contains the test scores from five students (see lines 7 through 11). Each element of a SortedList really is represented by the DictionaryEntry structure. This simple structure contains two public fields: Key and Value. Starting at line 17, we use a For Each ... Next loop to step through each DictionaryEntry element in our SortedList slTestScores. On line 18, we output the Key and Value, displaying the student's name and test score. Be sure to examine Figure 2.3 and notice that the displayed results are ordered by the value of the key.
On line 22, the ContainsKey method is used to see if Edward's score has been recorded; if so, it's reduced by ten points. (Poor Edward.) Note that we access the value of Edward's test score using the element's key—slTestScores("Edward")—just as if slTestScores were a Hashtable (line 23). On line 27, Sally's test score is removed from the SortedList via the Remove method.
Next, each remaining student's test score is upped by 5 percent. On lines 31 through 34, each test score is visited via a For ... Next loop (which is possible because SortedList elements can be accessed by an index). Because .NET collections are zero-based, notice that we loop from 0 to slTestScores.Count - 1 (line 31). On line 32, the value of each element is accessed via the GetValueList method, which returns a collection of values; this collection can then be indexed numerically.
On lines 37 through 40, another For ... Next loop is used to display the curved test results. On line 38, the GetKeyList method is used to return a collection of keys (which is then accessed by index); on line 39, the test results are outputted using the String.Format function. The format string passed to the String.Format function ("{0:#.#}") specifies that the first parameter following the format string (slTestScores.GetByIndex(iLoop), the test results) should only display one decimal place. Finally, on line 42, all the test results are erased with a single call to the Clear method.
Working with the Queue Class
ArrayLists, Hashtables, and SortedLists all have one thing in common—they allow random access to their elements. That is, a developer can programmatically read, write, or remove any element in the collection, regardless of its position. However, the Queue and Stack classes (the remaining two collections we'll examine) are unique in that they provide sequential access only. Specifically, the Queue class can only access and remove elements in the order they were inserted.
Adding, Removing, and Accessing Elements in a Queue
Queues are often referred to as First In, First Out (FIFO) data structures because the Nth element inserted will be the Nth element removed or accessed. It helps to think of the queue data structure as a line of people. There are two parts to a queue as there are two parts to any line up: the tail of the queue, where people new to the line start waiting, and the head of the queue, where the next person in line waits to be served. In a line, the person who is standing in line first will be first served; the person standing second will be served second, and so on. In a queue, the element that is added first will be the element that is removed or accessed first, whereas the second element added will be the second element removed or accessed.
The .NET Framework provides support for the queue data structure with the Queue class. To add an element to the tail, use the Enqueue method. To retrieve and remove an element from the head of a queue, use Dequeue. As with the other collection types we've examined thus far, the Queue class contains a Clear method to remove all elements. To simply examine the element at the head without altering the que