按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
To add elements to your list according to an order; use the following form (note that
ICollection doesn’t have this method)。
lst。Insert(0; New MyType())
This adds an element to the front of the list。 If you are adding elements at the beginning of
the list or somewhere in the list; it is better to use the LinkedList type; as it is more efficient。
Using the class List incurs an array copy resource penalty。
You can also add one list to another:
Dim lst As List(Of MyType) = New List(Of MyType)()
Dim lstToBeAdded As List(Of MyType)
lst。AddRange(lstToBeAdded)
lst。InsertRange(0; lstToBeAdded)
The AddRange() method is used to append the list lstToBeAdded to lst。 The InsertRange()
method inserts all of the elements in lstToBeAdded to the front of the list lst。
…………………………………………………………Page 268……………………………………………………………
246 CH AP T E R 9 ■ L E A R N IN G AB OU T L I ST S; D E L E G A T E S; A N D L A M B DA E X P R E S SI ON S
Delete an element from the list like this:
lst。Remove(existingMyType)
The Remove() method expects an instance of a type to remove from the list。
To delete a particular element at a particular index; use the following form。
lst。RemoveAt(0)
This code would remove the element at the front of the list。
Using a Key/Value Pair List
A key/value pair list is a list that has a cross…reference。 It is like a dictionary where you have
a word and associated meaning。 In puting terms; the word is a type and its definition is
another type。 The word is a key and the definition is a value。 A key/value pair definition would
be as follows; using the IDictionary interface and Dictionary class。
Dim dictionary As IDictionary(Of String; Object) = _
New Dictionary(Of String; Object)()
You could also use SortedDictionary; but that implies the elements within the list are sorted。
To add values to the dictionary; use the Add() method:
dictionary。Add(〃List〃; lst)
dictionary。Add(〃List To Be Added〃; lstToBeAdded)
When working with IDictionary objects; you might want to know whether or not a key is
available。 The following code is used to verify if a key exists。
If dictionary。ContainsKey(〃List〃) Then
。 。 。
End If
If you want to iterate the keys; use this form:
For Each key As String In dictionary。Keys
Next
Iterate the values as follows:
For Each value As Object In dictionary。Values
Next
Using a Stack
A Stack is a special list that behaves like a stack of paper on a table。 When you add three items
on the Stack; the last one added to the Stack is the first one off the Stack。 Here is an example of
using a Stack:
…………………………………………………………Page 269……………………………………………………………
C HA P TE R 9 ■ L E AR N I N G A B O U T L I ST S; DE L E G AT E S ; AN D L A M B D A E X PR E SSI O N S 247
Dim stack As Stack(Of String) = New Stack(Of String)()
stack。Push(〃first〃)
stack。Push(〃second〃)
stack。Push(〃third〃)
Dim popped As String = stack。Pop()
If popped。pareTo(〃third〃) = 0 Then
' This is what we expect
End If
The code demonstrates using the Push() method to push items on the stack and the Pop()
method to remove items from the stack。 Remember that Push() is an explicit addition; and
Pop() an explicit removal (though a call to Pop() returns the object removed from the stack so
you can do something with it; as shown in the code)。
If you want to know what is on the top of the stack; use Peek(); which acts like Pop(); except
it does not remove the item from the list。
Using a Queue
A Queue is another special type of list that behaves like a queue that you would encounter at
ticket counter。 As people start queuing; the first person to be served is the one at the front of the
line。 Here is an example of using a Queue:
Dim queue As Queue(Of String) = New Queue(Of String)()
queue。Enqueue(〃first〃)
queue。Enqueue(〃second〃)
queue。Enqueue(〃third〃)
Dim dequeued As String = queue。Dequeue()
If dequeued = 〃first〃 Then
' This is what we expect
End If
The Important Stuff to Remember
In this chapter; you learned about using delegates; lambda expressions; extension methods;
and lists。 The main items to remember are as follows:
o You are using Visual Basic 2008; and thus you should use the generics…based
collection classes。
o There are many different types of lists。 The main types are the simple object collection;
key/value collection; stack; and queue。
…………………………………………………………Page 270……………………………………………………………
248 CH AP T E R 9 ■ L E A R N IN G AB OU T L I ST S; D E L E G A T E S; A N D L A M B DA E X P R E S SI ON S
o generics…based classes are type…safe and have better performance than old…style
collections。
o Delegates help you define a generic method…calling mechanism without needing to
implement an interface。
o Delegates can be shared methods; instance methods; or module methods。 The only
important aspect to the method is to make sure the method signature matches the
delegate declaration。
o Lambda expressions are a specialized form of delegate method that enable you to write
deferred execution code。 The advantage of deferred execution is that the