C#数据结构和算法[An Introduction to Collections, Generics, and the Timing Class]

来源:互联网 发布:数据大小计算公式 编辑:程序博客网 时间:2024/06/03 22:40

An Introduction to Collections, Generics,and the Timing Class
集合,泛型,时间类的介绍
This book discusses the development and implementation of data structures
and algorithms using C#. The data structures we use in this book are found
in the .NET Framework class library System.Collections. In this chapter, we
develop the concept of a collection by first discussing the implementation of
our own Collection class (using the array as the basis of our implementation)
and then by covering the Collection classes in the .NET Framework.
本书讨论地是用C#开发或实现数据结构及算法,书中提及的数据结构都可以在System.Collections里找到。
本章,我们首先用自己的方式实现集合(用数组),然后再说.net Framework。

An important addition to C# 2.0 is generics. Generics allow the C# programmer
to write one version of a function, either independently or within a
class, without having to overload the function many times to allow for different
data types. C# 2.0 provides a special library, System.Collections.Generic,
that implements generics for several of the System.Collections data structures.
This chapter will introduce the reader to generic programming.
Finally, this chapter introduces a custom-built class, theTiming class, which
we will use in several chapters to measure the performance of a data structure
and/or algorithm. This class will take the place of Big O analysis, not because
Big O analysis isn’t important, but because this book takes a more practical
approach to the study of data structures and algorithms.
泛型是C#2.0的一个新特性,泛型允许程序员只需写一个函数(不管是独立地还是成员函数),不用重载很多次去适应各种数据类型。
C#2.0提供了一个库,System.Collections.Generic,它实现了一些集合类,本章介绍如何泛型编程。最后,本章介绍一个自定义类,
时间相关的类,用来代替O方法来测试算法的效率,不是说O方法不重要,是因为本书更注重实际的结果。

COLLECTIONS DEFINED
集合的定义
A collection is a structured data type that stores data and provides operations
for adding data to the collection, removing data from the collection, updating
data in the collection, as well as operations for setting and returning the values
of different attributes of the collection.
Collections can be broken down into two types: linear and nonlinear. A
linear collection is a list of elements where one element follows the previous
element. Elements in a linear collection are normally ordered by position
(first, second, third, etc.). In the real world, a grocery list is a good example
of a linear collection; in the computer world (which is also real), an array is
designed as a linear collection.
Nonlinear collections hold elements that do not have positional order
within the collection. An organizational chart is an example of a nonlinear
collection, as is a rack of billiard balls. In the computer world, trees, heaps,
graphs, and sets are nonlinear collections.
集合是一种存储数据并提供一些诸如增,删,查,改操作的数据结构。集合分线性和非线性的。
线性集合是一串连续的元素,元素间以位置排序。比如现实生活中的小卖部,比如计算机中的数组。
非线性集合内的元素位置并不一定连续,比如现实生活中的组织结构图,比如计算机中的树,堆,图,set。
Collections, be they linear or nonlinear, have a defined set of properties that
describe them and operations that can be performed on them. An example
of a collection property is the collections Count, which holds the number of
items in the collection. Collection operations, called methods, include Add
(for adding a new element to a collection), Insert (for adding a new element
to a collection at a specified index), Remove (for removing a specified element
from a collection), Clear (for removing all the elements from a collection),
Contains (for determining if a specified element is a member of a collection),
and IndexOf (for determining the index of a specified element in a
collection).

不管线性非线性集合,会定义一些属性和方法,比如count,保存集合内元素的个数,
比如添加,插入(在特定位置添加),删除,清空,查找是否存在,查找元素位置。

COLLECTIONS DESCRIBED
Within the two major categories of collections are several subcategories.
Linear collections can be either direct access collections or sequential access
collections, whereas nonlinear collections can be either hierarchical or
grouped. This section describes each of these collection types.
集合的描述
两个主要的集合分类下还有一些小分类。
线性集合可以是直接存储集合或序列化存储集合,非线性集合可以是层级或分组。本节将逐一讨论。

Direct Access Collections
The most common example of a direct access collection is the array.We define
an array as a collection of elements with the same data type that are directly
accessed via an integer index, as illustrated in Figure 1.1.
Item ø Item 1 Item 2 Item 3 . . . Item j Item n−1
FIGURE 1.1. Array.

直接存储集合
最常见的直接存储集合就是数组,用同样的数据类型定义一个数组时可以直接通过整数的索引来存取。
像 Item ø Item 1, Item 2, Item 3, . . . Item j, Item n−1

Arrays can be static so that the number of elements specified when the array
is declared is fixed for the length of the program, or they can be dynamic, where
the number of elements can be increased via the ReDim or ReDim Preserve
statements.
In C#, arrays are not only a built-in data type, they are also a class. Later
in this chapter, when we examine the use of arrays in more detail, we will
discuss how arrays are used as class objects.
数组可以是静态的,这样定义数组时需要指定数组的长度。也可以是动态的,可以通过redim来增加元素。
C#里。数组不只是一个内建数据类型,还是一个类,后面的章节就讨论做为类时是如何使用的。

We can use an array to store a linear collection. Adding new elements to an
array is easy since we simply place the new element in the first free position
at the rear of the array. Inserting an element into an array is not as easy (or
efficient), since we will have to move elements of the array down in order
to make room for the inserted element. Deleting an element from the end of
an array is also efficient, since we can simply remove the value from the last
element. Deleting an element in any other position is less efficient because,
just as with inserting, we will probably have to adjust many array elements
up one position to keep the elements in the array contiguous.We will discuss
these issues later in the chapter. The .NET Framework provides a specialized
array class, ArrayList, for making linear collection programming easier. We
will examine this class in Chapter 3.
数组可以存储线性集合,数组内添加元素很简单,就需要把新元素放到数组末尾的第一个空位置上。
插入元素倒没这么方便,因为需要移动很多元素去给新元素腾出位置来。从末尾删除元素很方便,只需要移除最后一个即可,
但是从不具体的位置删除就稍麻烦了,原理同插入元素一样,不多解释。
NET Framework专门提供了一个数组类,ArrayList,能为我们操作线性集合更简单,具体留到第三章说

Another type of direct access collection is the string. A string is a collection
of characters that can be accessed based on their index, in the same manner we
access the elements of an array. Strings are also implemented as class objects
in C#. The class includes a large set of methods for performing standard
operations on strings, such as concatenation, returning substrings, inserting
characters, removing characters, and so forth.We examine the String class in
Chapter 8.
另一个直接存储集合的类型是string,一个string对象是被索引的字符串的集合,我们可以像数组一样操作string。
C#也把string现成类了。类里有大量的方法来操作字符串,比如连接,返回字串,插入字符,删除字符,具体留到第八章说

C# strings are immutable, meaning once a string is initialized it cannot be
changed. When you modify a string, a copy of the string is created instead of
changing the original string. This behavior can lead to performance degradation
in some cases, so the .NET Framework provides a StringBuilder class that
enables you to work with mutable strings.We’ll examine the StringBuilder in
Chapter 8 as well.
C#里的string是不可变的,意思是一旦这个string对象初使化了值就不能改变了。
当你想要修改这个string对象时,其实是创建了这个string对象的副本,这样使得效率就降低了。
所以.net提供了一个StringBuilder类来操作可变的字符串。同样,具体的第八章再细说。
The final direct access collection type is the struct (also called structures
and records in other languages). A struct is a composite data type that holds
data that may consist of many different data types. For example, an employee
record consists of employee’ name (a string), salary (an integer), identification
number (a string, or an integer), as well as other attributes. Since storing each
of these data values in separate variables could become confusing very easily,
the language provides the struct for storing data of this type.
A powerful addition to the C# struct is the ability to define methods for
performing operations stored on the data in a struct. This makes a struct
somewhat like a class, though you can’t inherit or derive a new type from
a structure. The following code demonstrates a simple use of a structure
in C#:

 最后一个直接存储集合是struct(在其他语言里貌似叫结构体和记录)。struct是一个包含了很多不同数据类型的复杂数据类型。
比如,一条雇员记录包含 雇员姓名(string型),薪水(int型),工号(string或int型),等等。如果把这些变量分开存储的话很容易搞混。
所以C#提供了struct。C#里的struct的新特性是可以在struct里面定义方法.这样一来struct就有点像class了,但是不能从struct继承或派生新struct.
下面是一个struct的例子

原创粉丝点击