asp.net
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 queue, use the Peek method. As with all the other collections, the elements of a Queue can be iterated through using an enumerator or a For Each ... Next loop. Listing 2.1.4 illustrates some simple queue operations. The output is shown in Figure 2.4.
Listing 2.1.4 A Queue Supports First In, First Out Element Access and Removal
1: <script language="VB" runat="server">
2:
3: Sub Page_Load(sender as Object, e as EventArgs)
4: ' Create a Queue
5: Dim qTasks as New Queue()
6:
7: qTasks.Enqueue("Wake Up")
8: qTasks.Enqueue("Shower")
9: qTasks.Enqueue("Get Dressed")
10: qTasks.Enqueue("Go to Work")
11: qTasks.Enqueue("Work")
12: qTasks.Enqueue("Come Home")
13: qTasks.Enqueue("Eat Dinner")
14: qTasks.Enqueue("Go to Sleep")
15:
16: ' To determine if an element exists in the Queue,
17: ' use the Contains method
18: If Not qTasks.Contains("Shower") Then
19: ' Forgot to bathe!
20: Response.Write("<b><i>Stinky!</i></b>")
21: End If
22:
23: ' Output the list of tasks
24: lblTaskList.Text &= "<i>There are " & qTasks.Count & _
25: " tasks for today...</i><br>"
26:
27: Dim iCount as Integer = 1
28: Do While qTasks.Count > 0
29: lblTaskList.Text &= iCount.ToString() & ".) " & _
30: qTasks.Dequeue() & "<br>"
31: iCount += 1
32: Loop
33:
34:
35: ' At this point the queue is empty, since we've
36: ' Dequeued all of the elements.
37: End Sub
38:
39: </script>
40:
41: <html>
42: <body>
43:
44: <b>An In-Order List of Tasks for the Day:</b><br>
45: <asp:label runat="server" id="lblTaskList" />
46:
47: </body>
48: </html>