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
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 最新章:第230章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章