Common asp.net Code techniques (DPC&DWC Reference)--4

Source: Internet
Author: User
Tags count data structures reference
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 (= 7 lines 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, we have a for each ... Next Loop to step through each dictionaryentry element into our SortedList sltestscores. On line, we output the Key and Value, displaying the student ' s name and test score. Be sure to examine Figure 2.3 and notice this displayed results the are by the value of the key.

On line, the ContainsKey method was used to the 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 Sltestscore S were a Hashtable (line 23). On line, Sally ' s test score be removed from the SortedList via the Remove method.

Next, each remaining student ' s test score be upped by 5 percent. On lines through, the each test score was visited via a for ... Next Loop (which is possible because SortedList elements can being accessed by a index). Because. NET collections are zero-based, notice "we loop from 0 to Sltestscores.count-1" (line 31). On line, 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 Notoginseng through, another for ... Next loop is used to display the curved test results. On line, the Getkeylist method was used to return a collection of keys (which are then accessed by index); On line, the test results are outputted using the String.Format function. The format string passed to the String.Format function (' {0:#.#} ') specifies that the ' the ' the ' the ' the ' the ' ' parameter ' format String (Sltestscores.getbyindex (iloop), the test results) should only display one decimal place. Finally, on line, the "all" test results are erased with the "a" to "clear" method.

Working with the Queue Class
ArrayLists, Hashtables, and sortedlists all have one thing into common-they allow random access to their elements. That's, a developer can programmatically read, write, or remove any element in the collection, regardless of it position . However, the Queue and Stack classes (the remaining two collections we ' ll examine) are unique in that they provide sequent ial access only. Specifically, the Queue class can only have access and remove elements in the order they were inserted.

adding, removing, and accessing Elements in a Queue
Queues are often referred to as-as-a-I (FIFO) data structures because the nth element inserted would be the Nt h element removed or accessed. It helps to is the queue data structure as a line of people. There are two parts to a queue as There are two of the the queue, where parts new to the Start waiting, and the head of the queue, where the "next person" is waits to be served. In a line, the person who was standing in-line, and the person of the-is-is-a-served; The person standing second would be served second, and. In a queue, the element this is added to be the element of the, is removed or accessed, whereas the second Elem Ent added would be the second element removed or accessed.

The. NET Framework provides support for the queue data structure with the queue class. To add a element to the tail with the use of the Enqueue method. To retrieve and remove is the 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 in the head without altering the queue and use the Peek method. As with the collections, the elements of a Queue can is iterated through using an enumerator or a for each ... N Ext 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-in-a-in-,-i-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")
15:
: ' To determine if a element exists in the Queue,
: ' Use the Contains method
18:if not Qtasks.contains ("shower") Then
: ' Forgot to bathe!
20:response.write ("<b><i>Stinky!</i></b>")
21:end If
22:
: ' Output the list of tasks
24:lbltasklist.text &= "<i>there are" & Qtasks.count & _
: "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:
' At this point the ' \ Empty, since we ' ve
' Dequeued all of the elements.
37:end Sub
38:
: </script>
40:
: : <body>
43:
<b>an In-order List of Tasks for the day:</b><br>
<asp:label runat= "Server" id= "Lbltasklist"/>
46:
: </body>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.