博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#数据结构和算法 [Arrays and ArrayLists]

Posted on 2010-07-03 13:52  淡如水wp  阅读(1149)  评论(0编辑  收藏  举报

Arrays and ArrayLists

The array is the most common data structure, present in nearly all programming
languages. Using an array in C# involves creating an array object of
System.Array type, the abstract base type for all arrays. The Array class provides
a set of methods for performing tasks such as sorting and searching that
programmers had to build by hand in the past.
An interesting alternative to using arrays in C# is the ArrayList class. An
arraylist is an array that grows dynamically as more space is needed. For
situations where you can’t accurately determine the ultimate size of an array,
or where the size of the array will change quite a bit over the lifetime of a
program, an arraylist may be a better choice than an array.
In this chapter, we’ll quickly touch on the basics of using arrays in C#,
then move on to more advanced topics, including copying, cloning, testing
for equality and using the static methods of the Array and ArrayList
classes.

Arrays 和 ArrayLists
数组是最常用的数据结构,几乎在所有的编程语言中都会出现。在C#里用数组的话需要建立一个System.Array类型的对象,它是所有数组的抽象基类。
Array类提供了一组过去需要程序员手动完成的方法,比如排序和查找。
另一个可以建立数组的替代品是ArrayList,ArrayList是可以按需要动态增长的数组。
在不能确定一个数组的大小的情况下,或在程序的生命周期内数组的大下需要改变时,ArrayList是个好的选择。
本章会快速了解一下C#里数组的基础,然后转向深层的主题,包括拷贝,克隆,比较是否相等以及用Array和ArrayList的静态方法。
ARRAY BASICS
Arrays are indexed collections of data. The data can be of either a built-in
type or a user-defined type. In fact, it is probably the simplest just to say that
array data are objects. Arrays in C# are actually objects themselves because
they derive from the System.Array class. Since an array is a declared instance

of the System.Array class, you have the use of all the methods and properties
of this class when using arrays.
数组基础
数组是一组索引数据的集合。这些数据可以是任何内建类型。其实,可以简单地认为数组数据是对象。
C#里的数组就是对象本身,因为它派生自System.Array类。
声明一个System.Array类的一个实例后,你就可以用这个类的所有方法了。

Declaring and Initializing Arrays
Arrays are declared using the following syntax:
type[] array-name;
where type is the data type of the array elements. Here is an example:
string[] names;
A second line is necessary to instantiate the array (since it is an object of
System.Array type) and to determine the size of the array. The following line
instantiates the names array just declared:
names = new string[10];
and reserves memory for five strings.
声明和初使化数组
数组声明时用下面的格式:

type[] array_name;
type就是数据类型,比如
string[] names;

第二行需要实例化这个数组(因为是System.Array类型)并指定数组的大小。代码如下:
names = new string[10];

这样就为10个字符串分配了内存。
You can combine these two statements into one line when necessary to do
so:
string[] names = new string[10];
There are times when you will want to declare, instantiate, and assign data
to an array in one statement. You can do this in C# using an initialization
list:
int[] numbers = new int[] {1,2,3,4,5};
The list of numbers, called the initialization list, is delimited with curly braces,
and each element is delimited with a comma. When you declare an array
using this technique, you don’t have to specify the number of elements. The
compiler infers this data from the number of items in the initialization
list.
也可以两句合在一起写:

string[] names = new string[10];
有时候你想把声明,实例化,指定数据三个步骤同时进行,那就这样子:
int[] numbers = new int[] {1,2,3,4,5};
这一串数字,叫做初使化列表,用花括号括起来,每个元素用逗号隔开。
当用这种方式声明数组时,不用指定数组大小,编译器会自动根据初使化列表的个数来指定。

Setting and Accessing Array Elements
Elements are stored in an array either by direct access or by calling the Array
class method SetValue. Direct access involves referencing an array position by
index on the left-hand side of an assignment statement:
Names[2] = "Raymond";
Sales[19] = 23123;
The SetValue method provides a more object-oriented way to set the value
of an array element. The method takes two arguments, an index number and
the value of the element.
names.SetValue[2, "Raymond"];
sales.SetValue[19, 23123];
Array elements are accessed either by direct access or by calling the
GetValue method. The GetValue method takes a single argument—an index.
myName = names[2];
monthSales = sales.GetValue[19];
设定和存取数组元素
数组元素的存储既可以直接存储,也可以调用SetValue方法,直接存储方法需要用索引指定数组内的位置,向下面这样:

Names[2] = "Raymond";
Sales[19] = 23123;
SetValue方法提供了一个更面向对象的做法去设置数组元素的值,此方法有两个参数,一个是索引数,另一个是值。
names.SetValue[2, "Raymond"];
sales.SetValue[19, 23123];
数组元素的取出既可以直接取得,也可以调用GetValue方法,GetValue用一个简单的参数:索引数

myName = names[2];
monthSales = sales.GetValue[19];

It is common to loop through an array in order to access every array element
using a For loop. A frequent mistake programmers make when coding the loop
is to either hard-code the upper value of the loop (which is a mistake because
the upper bound may change if the array is dynamic) or call a function that
accesses the upper bound of the loop for each iteration of the loop:
(for int i = 0; i <= sales.GetUpperBound(0); i++)
totalSales = totalSales + sales[i];
Methods and Properties for Retrieving Array Metadata
The Array class provides several properties for retrieving metadata about an
array:
 Length: Returns the total number of elements in all dimensions of an array.
 GetLength: Returns the number of elements in specified dimension of an
 Rank: Returns the number of dimensions of an array.
 GetType: Returns the Type of the current array instance.
通过循环去取得数组内的每个元素是很常见的。程序员写循环时会经常犯一个错误:就是写死了循环的上限或调用取得上限的方法。
错误主要是因为数组的上限是动态的
for (int i = 0; i <= sales.GetUpperBound(0); i++)
totalSales = totalSales + sales[i];
检索数组元数据的方法和属性:
Length:返回数组元素的所有个数。
GetLength:返回数组元素一维内的个数。
Rank:返回维数
GetType:返回当前数组实例的类型

The Length method is useful for counting the number of elements in a
multidimensional array, as well as returning the exact number of elements in
the array. Otherwise, you can use the GetUpperBound method and add one
to the value.
Since Length returns the total number of elements in an array, the
GetLength method counts the elements in one dimension of an array. This
method, along with the Rank property, can be used to resize an array at runtime
without running the risk of losing data. This technique is discussed later
in the chapter.
The GetType method is used for determining the data type of an array in
a situation where you may not be sure of the array’s type, such as when the
array is passed as an argument to a method. In the following code fragment,
we create a variable of type Type, which allows us to use call a class method,
IsArray, to determine if an object is an array. If the object is an array, then the
code returns the data type of the array.
Length方法在统计多维数组内元素的个数时很有用。要不然就用GetUpperBound方法+1取这个值
因为Length方法返回的是数组内所有元素的个数,GetLength方法返回的是一维内数组的个数。
Rank属性可以在运行时改变数组的大小而不用担心丢失数据,这个在章节后面详细讨论。
GetType方法是当你确定不了数组类型时用来判定数据类型的方法,比如数组是入参。
下面的代码,会创建一个Type类型的变量,允许我们调用一个类方法:IsArray()来判定这个对象是否是数组,
如果是数组的话,那么就返回数组的数据类型
int[] numbers;
numbers = new int[] {0,1,2,3,4};
Type arrayType = numbers.GetType();
if (arrayType.IsArray)
Console.WriteLine("The array type is: {0}", arrayType);
else
Console.WriteLine("Not an array");
Console.Read();

The GetType method returns not only the type of the array, but also lets us
know that the object is indeed an array. Here is the output from the code:
The array type is: System.Int32[]
The brackets indicate the object is an array. Also notice that we use a format
when displaying the data type. We have to do this because we can’t convert
the Type data to string in order to concatenate it with the rest of the displayed
string.
GetType方法不仅返回数组的类型,也告诉我们数组其实是一个对象,下面是输出的代码:
The array type is: System.Int32[]
方括号说明这个对象是个数组,也注意一下当显示数组类型时我们使用的格式。
知道了这个,我们不能转换把这个类型转换成string去连接剩下的字符串

30 ARRAYS AND ARRAYLISTS
Multidimensional Arrays
So far we have limited our discussion to arrays that have just a single dimension.
In C#, an array can have up to 32 dimensions, though arrays with more
than three dimensions are very rare (and very confusing).
Multidimensional arrays are declared by providing the upper bound of each
of the dimensions of the array. The two-dimensional declaration:
int[,] grades = new int[4,5];
declares an array that consists of 4 rows and 5 columns. Two-dimensional
arrays are often used to model matrices.
You can also declare a multidimensional array without specifing the dimension
bounds. To do this, you use commas to specify the number of dimensions.
double[,,] sales;
For example,
double[,] Sales;
declares a two-dimensional array, whereas
double[,,] sales;
declares a three-dimensional array. When you declare arrays without providing
the upper bounds of the dimensions, you have to later redimension the
array with those bounds:
sales = new double[4,5];
多维数组
目前我们的讨论都限于一维数组,C#里,可以用最多32维的数组,虽然3维以上的数组就不常用了(还混乱的很)。
多维数组是用指定了每维上限的数组声明的,2维的声明:
int[,] grades = new int[4,5];
声明一个4行5列的数组,2维数组一般用来模拟矩阵。
你也可以声明一个不指定每一维的上限的多维数组,用逗号分开就行了。
举个例子:
double[,] Sales;
声明一个2维数组,
double[,,] sales;
声明一个3维数组,当你声明数组不指定某维的上限时,用的时候要指定
sales = new double[4,5];

Multidimensional arrays can be initialized with an initialization list. Look
at the following statement:
Int[,] grades = new int[,] {{1, 82, 74, 89, 100},
{2, 93, 96, 85, 86},
{3, 83, 72, 95, 89},
{4, 91, 98, 79, 88}}
First, notice that the upper bounds of the array are not specified. When you
initialize an array with an initialization list, you can’t specify the bounds of
the array. The compiler computes the upper bounds of each dimension from
the data in the initialization list. The initialization list itself is demarked with
curly braces, as is each row of the array. Each element in the row is delimited
with a comma.
多维数组也可以用初使化列表,看下面的代码:
Int[,] grades = new int[,] {{1, 82, 74, 89, 100},
{2, 93, 96, 85, 86},
{3, 83, 72, 95, 89},
{4, 91, 98, 79, 88}}
首先,注意数组的上限是没有指定的,当用初使化列表来初使化一个数组时,
不能指定该数组的上限。编译器会通过初使化列表计算每一维的上限。
初使化列表本身是一对花括号,每一行,每一个元素都用逗号分开。

Accessing the elements of a multidimensional array is similar to accessing
the elements of a one-dimensional array. You can use the traditional array
access technique,
grade = Grades[2,2];
Grades[2,2] = 99
or you can use the methods of the Array class:
grade = Grades.GetValue[0,2]
You can’t use the SetValue method with a multidimensional array because the
method only accepts two arguments: a value and a single index.
取得多数组的元素和一维是差不多的,传统技术是这样的:

grade = Grades[2,2];
Grades[2,2] = 99
或者也可以用GetValue方法
grade = Grades.GetValue[0,2];

但不能用SetValue方法来设置多维数组的数据,因为这个方法只有两个参数。
It is a common operation to perform calculations on all the elements of
a multidimensional array, though often based on either the values stored in
the rows of the array or the values stored in the columns of the array. Using
the Grades array, if each row of the array is a student record, we can calculate
the grade average for each student as follows:
在多维数组里计算所有元素是很常见的操作,虽然常常时是基于某一行或某一列的数据。
用Grades这个数组,如果数组的每一行是一条学生记录,我们可以计算年级里每个学生的平均分:

int[,] grades = new int[,] {{1, 82, 74, 89, 100},
{
2, 93, 96, 85, 86},
{
3, 83, 72, 95, 89},
{
4, 91, 98, 79, 88}};
int last_grade = grades.GetUpperBound(1);
double average = 0.0;
int total;
int last_student = grades.GetUpperBound(0);
for(int row = 0; row <= last_student; row++) {
total
= 0;
for (int col = 0; col <= last_grade; col++)
total
+= grades[row, col];
average
= total / last_grade;
Console.WriteLine(
"Average: " + average);
}
}

 

 

Parameter Arrays
Most method definitions require that a set number of parameters be provided
to the method, but there are times when you want to write a method definition
that allows an optional number of parameters. You can do this using a
construct called a parameter array.
A parameter array is specified in the parameter list of a method definition
by using the keyword ParamArray. The following method definition allows
any amount of numbers to be supplied as parameters, with the total of the
numbers returned from the method:
参数数组
很多方法的定义需要提供好几个参数,但是有时候你想写一个参数个数可选的方法。
你可以声明一个参数数组。参数数组是用关键字ParamArray定义参数列表的。
下面这个方法允许任意个数的参数,返回值是参数的个数。

static int sumNums(params int[] nums) {
int sum = 0;
for(int i = 0; i <= nums.GetUpperBound(0); i++)
sum
+= nums[i];
return sum;
}

This method will work with the either of the following calls:
total = sumNums(1, 2, 3);
total = sumNums(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
When you define a method using a parameter array, the parameter array
arguments have to be supplied last in the parameter list in order for the
compiler to be able to process the list of parameters correctly. Otherwise, the
compiler wouldn’t know the ending point of the parameter array elements
and the beginning of other parameters of the method.
这个方法可以被以下代码调用:

total = sumNums(1, 2, 3);
total = sumNums(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
当你用参数数组定义一个方法时,参数数组必须放在此方法所有参数的最后,
这样编译器可以正确处理参数,否则,编译器不知道参数个数什么时候结束。

Jagged Arrays
When you create a multidimensional array, you always create a structure that
has the same number of elements in each of the rows. For example, look at
the following array declaration:
int sales[,] = new int[12,30]; ' Sales for each day of each month
This array assumes each row (month) has the same number of elements
(days), when we know that some months have 30 days, some have 31, and
one month has 29. With the array we’ve just declared, there will be several
empty elements in the array. This isn’t much of a problem for this array, but
with a much larger array we end up with a lot of wasted space.
不规则数组
当你创建一个多维数组时,你一直创建的是每一行的元素个数相同的结构。打个比方,你看下面的代码:

int sales[,] = new int[12,30];//销售员每个月每天的情况
假设每行(每月)有同样的元素个数(天数),而实际上有的月31天,有的30天,有的29.
这样数据就有空位置,在这个数组里问题倒不大,但是一旦数据量大了以后就很浪费了

The solution to this problem is to use a jagged array instead of a two dimensional
array. A jagged array is an array of arrays where each row of
an array is made up of an array. Each dimension of a jagged array is a onedimensional
array.We call it a “jagged” array because the number of elements
in each row may be different. A picture of a jagged array would not be square
or rectangular, but would have uneven or jagged edges.
A jagged array is declared by putting two sets of parentheses after the array
variable name. The first set of parentheses indicates the number of rows in
the array. The second set of parentheses is left blank. This marks the place for
the one-dimensional array that is stored in each row. Normally, the number
of rows is set in an initialization list in the declaration statement, like this:
int[][] jagged = new int[12][];

解决问题的方法是使用一个不规则数组来代替二维数组。
不规则数组是一个数组的每一行也是数组的数组。不规则数组的每一维是一维数组,叫它“不规则”是因为每一行的个数不同。
不规则数组的样子可能不是一个矩形,但是有不规则的边。
不规则数组用数组名后的两对括号声明。第一对括号指定行数,第二对是留空的。
int[][] jagged = new int[12][];

This statement looks strange, but makes sense when you break it down. jagged
is an Integer array of 12 elements, where each of the elements is also an Integer
array. The initialization list is actually just the initialization for the rows of
the array, indicating that each row element is an array of 12 elements, with
each element initialized to the default value.
Once the jagged array is declared, the elements of the individual row
arrays can be assigned values. The following code fragment assigns values
to jaggedArray:
jagged[0][0] = 23;
jagged[0][1] = 13;
. . .
jagged[7][5] = 45;
The first set of parentheses indicates the row number and the second set
indicates the element of the row array. The first statement accesses the first
element of the first array, the second element access the second element of
the first array, and the third statement accesses the sixth element of the eighth
array.
这句代码看上去有点怪,但是分开来理解就明白了。这个不规则数组有12个元素。
每个元素也是一个数组。初使化列表其实就是为了初使化每一行数据,说明每一行元素是有12个元素的数据,每个元素都初使成默认值。
一旦不规则数组声明,每一行的元素就可以指定指了,下面的代码就是设置值的:
jagged[0][0] = 23;
jagged[0][1] = 13;
. . .
jagged[7][5] = 45;
第一对括号指明行号,第二对括号指明这一行的某一个元素。
第一句取第一行的第一个元素,第二句取第一行的第二个元素。
第三句取第6行的第8个元素。
For an example of using a jagged array, the following program creates an
array named sales (tracking one week of sales for two months), assigns sales
figures to its elements, and then loops through the array to calculate the
average sales for one week of each of the two months stored in the array.
这里有一个用不规则数组的例子,下面的程序创建一个叫做sales的数组,循环用来计算这两个月每周的平均销售情况。

using System;
class class1 {
static void Main[] {
int[] Jan = new int[31];
int[] Feb = new int[29];
int[][] sales = new int{Jan, Feb};
int month, day, total;
double average = 0.0;
sales[
0][0] = 41;
sales[
0][1] = 30;
sales[
0][0] = 41;
sales[
0][1] = 30;
sales[
0][2] = 23;
sales[
0][3] = 34;
sales[
0][4] = 28;
sales[
0][5] = 35;
sales[
0][6] = 45;
sales[
1][0] = 35;
sales[
1][1] = 37;
sales[
1][2] = 32;
sales[
1][3] = 26;
sales[
1][4] = 45;
sales[
1][5] = 38;
sales[
1][6] = 42;
for(month = 0; month <= 1; month++) {
total
= 0;
for(day = 0; day <= 6; day++)
total
+= sales[month][day];
average
= total / 7;
Console.WriteLine(
"Average sales for month: " +
month
+ ": " + average);
}
}
}


The ArrayList Class
Static arrays are not very useful when the size of an array is unknown in
advance or is likely to change during the lifetime of a program. One solution
to this problem is to use a type of array that automatically resizes itself
when the array is out of storage space. This array is called an ArrayList
and it is part of the System.Collections namespace in the .NET Framework
library.
An ArrayList object has a Capacity property that stores its size. The initial
value of the property is 16. When the number of elements in an ArrayList
reaches this limit, the Capacity property adds another 16 elements to the
storage space of the ArrayList. Using an ArrayList in a situation where the
number of elements in an array can grow larger, or smaller, can be more
efficient than using ReDim Preserver with a standard array.
As we discussed in Chapter 1, an ArrayList stores objects using the Object
type. If you need a strongly typed array, you should use a standard array or
some other data structure.
ArrayList类
在不知道数组大小的情况或运行时数组大小要改变的情况下,静态数组就不是那么有用了。
一个解决方是用一个空间不够用时可以自增长的数组类型,这个数组就是ArrayList,它也是System.Collections里的一部分。
ArrayList类的对象有一个Capacity属性用来存储数组大小,初使值是16,当数组的元素接近16个时,Capacity会加16,
当然同时数组空间也变大了。需要用ArrayList的情况是这样的:数组可能会变大或变小时,它比在标准数组里用ReDim更高效。
这个第一章也说过,ArrayList存储的是对象类型,如果需要强类型数组,你应该用标准数组或其他数据结构。

Members of the ArrayList Class
The ArrayList class includes several methods and properties for working with
ArrayLists. Here is a list of some of the most commonly used methods and
properties:
 Add(): Adds an element to the ArrayList.
 AddRange(): Adds the elements of a collection to the end of the ArrayList.
 Capacity: Stores the number of elements the ArrayList can hold.
 Clear(): Removes all elements from the ArrayList.
 Contains(): Determines if a specified item is in the ArrayList.
 CopyTo(): Copies the ArrayList or a segment of it to an array.
 Count: Returns the number of elements currently in the ArrayList.
 GetEnumerator(): Returns an enumerator to iterate over the ArrayList.
 GetRange(): Returns a subset of the ArrayList as an ArrayList.
 IndexOf(): Returns the index of the first occurrence of the specified
item.
 Insert(): Insert an element into the ArrayList at a specified index.
 InsertRange(): Inserts the elements of a collection into the ArrayList starting
at the specified index.
 Item(): Gets or sets an element at the specified index.
 Remove(): Removes the first occurrence of the specified item.
 RemoveAt(): Removes an element at the specified index.
 Reverse(): Reverses the order of the elements in the ArrayList.
 Sort(): Alphabetically sorts the elements in the ArrayList.
 ToArray(): Copies the elements of the ArrayList to an array.
 TrimToSize(): Sets the capacity of the ArrayList to the number of elements
in the ArrayList.
ArrayList类的成员
以下是些常用的方法或属性:
 Add(): 向ArrayList添加一个元素.
 AddRange(): 向ArrayList添加一组元素.
 Capacity: 存储ArrayList可以容纳元素的个数
 Clear(): 从ArrayList中移除一个元素.
 Contains(): 判断是否有指定的元素存在于ArrayList.
 CopyTo(): 复制ArrayList或是ArrayList的一部分到一个Array类.
 Count: 返回ArrayList内的元素个数.
 GetEnumerator(): 返回一个ArrayList的迭代器.
 GetRange(): 返回ArrayList的一个子集.
 IndexOf(): 返回第一个发现的指定元素的索引.
 Insert(): 向ArrayList的指定位置插入一个元素.
 InsertRange(): 向ArrayList的指定位置插入一组元素
 Item(): 设置或取得某个指定位置的值.
 Remove(): 移除第一个发现的指定元素.
 RemoveAt(): 移除指定位置的元素.
 Reverse(): 逆序.
 Sort(): 按字母排序.
 ToArray(): 复制所有元素到一个Array类.
 TrimToSize(): 将Capacity值设置成与数组元素个数相等的值.

Using the ArrayList Class
ArrayLists are not used like standard arrays. Normally, items are just added
to an ArrayList using the Add method, unless there is a reason why an item
should be added at a particular position, in which case the Insert method
should be used. In this section, we examine how to use these and the other
members of the ArrayList class.
The first thing we have to do with an ArrayList is declare it, as follows:
ArrayList grades = new ArrayList();
Notice that a constructor is used in this declaration. If an ArrayList is not
declared using a constructor, the object will not be available in later program
statements.
Objects are added to an ArrayList using the Add method. This method
takes one argument—an Object to add to the ArrayList. The Add method also
returns an integer indicating the position in the ArrayList where the element
was added, though this value is rarely used in a program. Here are some
examples:
使用ArrayList类
ArrayList不像标准的数组。一般来说,添加元素用Add(),除非在特定位置添加,那就用Insert()。
这里,我们测试一下怎么使用ArrayList的其他方法。
首先声明一个对象ArrayList grades = new ArrayList();
注意这里用了构造函数,如果ArrayList没有用构造函数,这个ArrayList对象就不能使用。
用Add方法添加元素,这个方法只有一个参数,就是要添加到ArrayList的对象。
Add方法有返回值,返回的插入的位置,只是很少用而已。下面是例子:

grades.Add(100);
grades.Add(84);
int position;
position = grades.Add(77);
Console.WriteLine("The grade 77 was added at position:
" + position);

The objects in an ArrayList can be displayed using a For Each loop. The
ArrayList has a built-in enumerator that manages iterating through all the
objects in the ArrayList, one at a time. The following code fragment demonstrates
how to use a For Each loop with an ArrayList:
ArrayList对象可以用Foreach来遍历,ArrayList有一个内建的枚举器
int total = 0;
double average = 0.0;
foreach (Object grade in grades)
total += (int)grade;
average = total / grades.Count;
Console.WriteLine("The average grade is: " + average);

If you want to add an element to an ArrayList at a particular position,
you can use the Insert method. This method takes two arguments: the index
to insert the element, and the element to be inserted. The following code
fragment inserts two grades in specific positions in order to preserve the
order of the objects in the ArrayList:
如果你要添加元素到特定位置,用Insert方法,有两个参数,位置和值
grades.Insert(1, 99);
grades.Insert(3, 80);

You can check the current capacity of an ArrayList by calling the Capacity
property and you can determine how many elements are in an ArrayList by
calling the Count property:
查看ArrayList当前的容量可以用Capacity属性,看数组个数可以用Count属性
Console.WriteLine("The current capacity of grades is:
" + grades.Capacity);
Console.WriteLine("The number of grades in grades is:
" + grades.Count);

There are several ways to remove items from an ArrayList. If you know
the item you want to remove, but don’t know what position it is in, you can
use the Remove method. This method takes just one argument—an object to
remove from the ArrayList. If the object exists in the ArrayList, it is removed. If
the object isn’t in the ArrayList, nothing happens.Whena method like Remove
is used, it is typically called inside an If–Then statement using a method that
can verify the object is actually in the ArrayList, such as the Contains method.
Here’s a sample code fragment:
这里有些其他的方法移除元素,如果你知道要移除的元素,但不知道位置,用Remove方法,只有一个参数,就是这个你要移除的对象。
如果这个对象存在于ArrayList中,会被移除,否则,什么都不做。就像是以下的代码。
if (grades.Contains(54))
grades.Remove(54)
else
Console.Write("Object not in ArrayList.");

If you know the index of the object you want to remove, you can use the
RemoveAt method. This method takes one argument—the index of the object
you want to remove. The only exception you can cause is passing an invalid
index to the method. The method works like this:
如果知道要移除的对象的位置,用RemoveAt方法,这个方法有一个参数:对象的索引。
如果传入无效的索引则会异常。
grades.RemoveAt(2);

You can determine the position of an object in an ArrayList by calling the
IndexOf method. This method takes one argument, an object, and returns
the object’s position in the ArrayList. If the object is not in the ArrayList, the
method returns -1. Here’s a short code fragment that uses the IndexOf method
in conjunction with the RemoveAt method:
取得一个对象的索引用IndexOf方法,有一个参数,就是这个对象。返回这个对象的索引。如果不存在,返回-1
int pos;
pos = grades.IndexOf(70);
grades.RemoveAt(pos);

In addition to adding individual objects to an ArrayList, you can also add
ranges of objects. The objects must be stored in a data type that is derived
from ICollection. This means that the objects can be stored in an array, a
Collection, or even in another ArrayList.
There are two different methods you can use to add a range to an ArrayList.
These methods are AddRange and InsertRange. The AddRange method adds
the range of objects to the end of the ArrayList, and the InsertRange method
adds the range at a specified position in the ArrayList.
The following program demonstrates how these two methods are used:
如果要添加对象集合的话,这组对象必须派生自ICollection,意思是说,这组对象可以存在于数组,Collection或者ArrayList里。
这里有两个不同的方法去添加一组对象到ArrayList。AddRange和InsertRange,AddRange添加元素到末尾,InsertRange方法添加到指定位置。

using System;
using System.Collections;
class class1 {
static void Main() {
ArrayList names
= new ArrayList();
names.Add(
"Mike");
names.Add(
"Beata");
names.Add(
"Raymond");
names.Add(
"Bernica");
names.Add(
"Jennifer");
Console.WriteLine(
"The original list of names: ");
Array Basics
39
foreach (Object name in names)
Console.WriteLine(name);
Console.WriteLine();
string[] newNames = new string[] {"David", "Michael"};
ArrayList moreNames
= new ArrayList();
moreNames.Add(
"Terrill");
moreNames.Add(
"Donnie");
moreNames.Add(
"Mayo");
moreNames.Add(
"Clayton");
moreNames.Add(
"Alisa");
names.InsertRange(
0, newNames);
names.AddRange(moreNames);
Console.WriteLine(
"The new list of names: ");
foreach (Object name in names)
Console.WriteLine(name);
}
}

 

 

The output from this program is:
运行结果如下:
David
Michael
Mike
Bernica
Beata
Raymond
Jennifer
Terrill
Donnie
Mayo
Clayton
Alisa
The first two names are added at the beginning of the ArrayList because
the specified index is 0. The last names are added at the end because the
AddRange method is used.
Two other methods that many programmers find useful are the ToArray
method and the GetRange method. The GetRange method returns a range of
objects from the ArrayList as another ArrayList. The ToArray method copies
all the elements of the ArrayList to an array. Let’s look first at the GetRange
method.
前面两个名字在ArrayList的开头是因为指定的索引是0.最后的那些名字在末尾是因为用的AaddRange。
还有两个常用的方法是ToArray和GetRange方法。GetRange方法返回ArrayList里的一组对象做为新的ArrayList。
ToArray方法复制所有元素到一个array元素,来一起看一下吧

The GetRange method takes two arguments: the starting index and the
number of elements to retrieve from the ArrayList. GetRange is not destructive,
in that the objects are just copied from the original ArrayList into the
new ArrayList. Here’s an example of how the method works, using the same
aforementioned program:
GetRange方法有两个参数:开始的索引和要取得的元素个数。
GetRange方法没有破坏性,只是取复本而已。下面是例子代码

ArrayList someNames = new ArrayList();
someNames
= names.GetRange(2,4);
Console.WriteLine(
"someNames sub-ArrayList: ");
foreach (Object name in someNames)
Console.WriteLine(name);

The output from this program fragment is:
运行结果如下:
Mike
Bernica
Beata
Raymond

The ToArray method allows you to easily transfer the contents of an
ArrayList to a standard array. The primary reason you will use the ToArray
method is because you need the faster access speed of an array.
The ToArray method takes no arguments and returns the elements of the
ArrayList to an array. Here’s an example of how to use the method:
ToArray方法允许你简单地把ArrayList的内容转换成标准数组。
用这个方法的主要原因是你需要更快的存取数据。
ToArray方法没有参数并且返回所有元素到一个数据。下面是例子代码

Object[] arrNames;
arrNames
= names.ToArray();
Console.WriteLine(
"Names from an array: ");
for(int i = 0; i <= arrNames.GetUpperBound(0); i++)
Console.WriteLine(arrNames[i]);

The last part of the code fragment proves that the elements from the ArrayList
have actually been stored in the array arrNames.
代码的最后部分证明ArrayList里的元素被存储在了数组里。
SUMMARY
The array is the most commonly used data structure in computer programming.
Most, if not all, computer languages provide some type of built-in array.
For many applications, the array is the easiest data structure to implement
and the most efficient. Arrays are useful in situations where you need direct
access to “far away” elements of your data set.
The .NET Framework introduces a new type of array called an ArrayList.
ArrayLists have many of the features of the array, but are somewhat more
powerful because they can resize themselves when the current capacity of the
structure is full. The ArrayList also has several useful methods for performing
insertions, deletions, and searches. Since C# does not allow a programmer to
dynamically resize an array as you can in VB.NET, the ArrayList is a useful data
structure for situations where you can’t know in advance the total number of
items for storage.
总结:

数组是最常用的数据结构。几乎所有的编程语言都内置了数组类型。
很多应用程序里,数组是最简单最有效的数据结构,数组是很有用的。
.net有一个新的数组类型叫做ArrayList,基本上和数组差不多,但更强的是它可以重新设置容量。
ArrayList还有一些很有用的方法,比如插入,删除,以及查找。
因为C#不允许程序员像VB.net那样动态变更数组的大小,ArrayList在不知道数组大小的情况下是很有用的数据结构。