特性
数组是一个无序的元素序列。数组元素存储在一个连续性的内存块中,并可使用一个整数索引来访问。
C# 声明数组变量时,数组的大小不是声明的一部分。这点与C/C++有些区别。
1
|
int [] dogs; // 声明数组 // 声明时不需要指定数组的大小
|
只有在实际创建数组实例的时候,才需要指定数组的大小。创建数组实例时编译器默认将数组元素初始化为0,null,false(依元素类型不同)。
1
|
pins = new int [4]; // 只有在实际创建数组实例的时候,才需要指定数组的大小。
|
以上两个语句运行后结果如下:
数组实例的大小不一定是常量,它可以在运行时计算。
int size = int.Parse(Console.ReadLine()); int[] pins = new int[size];
某些情况下,数组大小可能为 0,这个设计是有意义的。大小为 0 的数组不是一个 null(空)数组。除了基本数据类型,数组元素还可以是结构、枚举或者类。
大括号中的值不一定是常量,它们可以是在运行时计算的值。
1
2
3
4
5
|
Random r = new Random();
int [] pins = new int [4]{r.Next()%10,
r.Next()%10,
r.Next()%10,
r.Next()%10};
|
初始化
初始化数组变量。大括号中的值的数量必须和要创建的数组实例的大小完全匹配!
1
2
3
|
int [] pins = new int [3] { 1, 2, 3, 4 }; // 编译时错误
int [] pins = new int [4] { 1, 2, 3 }; // 编译时错误
int [] pins = new int [4] { 1, 2, 3, 4 }; // 正确
|
初始化一个数组变量时,实际上可省略 new 表达式和数组的大小,编译器将根据初始值的数量来计算大小。
1
|
int [] pins = { 1, 2, 3, 4 }; // 正确
|
创建一个由结构构成的数组时,为了初始化数组的每个结构,可以调用结构构造器。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
struct Time
{ private int hours, minutes, seconds;
public Time( int hh, int mm)
{
hours = hh;
minutes = mm;
seconds = 0;
}
} Time[] schedule = { new Time(12, 30), new Time(5, 30)}; // 调用自定义的结构构造器
|
隐式类型的数组创建
匿名类(anonymous class)是一个没有名字的类,只能包含 public 字段,字段必须全部初始化,不可以是 static 字段,而且不能在其中指定方法。
可以创建隐式类型的数组。如果在声明数组的时候指定了一个初始值列表,就可以让 C# 编译器自己推断数组中的元素的类型。
1
2
3
4
5
6
7
8
9
10
11
|
var names = new []{ "Steven" , "Lily" , "Tom" , "Miley" }; // C# 推断 names 是一个 string 类型
Console.WriteLine(names[0]); Console.WriteLine(names[3]); // 隐式类型转换的数组尤其适用于匿名类型 var people = new []{ new {Name = "Steven" , Age = 27},
new {Name = "Lily" , Age = 25},
new {Name = "Tom" , Age = 8},
new {Name = "Miley" , Age = 6}};
// 数组中的每个元素都是匿名类型 // 并且每个元素都必须指定相同的匿名类型 |
访问及遍历数组有如下方法,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace arrayType
{ class Program
{
static void Main( string [] args)
{
string [] names = new []{ "Steven" , "Lily" , "Tom" , "Miley" };
Console.WriteLine(names[0]);
Console.WriteLine(names[3]);
Console.WriteLine();
Console.WriteLine( "用 for 遍历字符串数组" );
for ( int i = 0; i < names.Length; i++)
// length 是属性而不是方法,不要加 '()'
{
Console.WriteLine(names[i]);
}
Console.WriteLine();
Console.WriteLine( "用 foreach 遍历 string 数组" );
// foreach 是遍历数组的首选方式
foreach ( string name in names)
{
Console.WriteLine(name);
}
var people = new []{ new {Name = "Steven" , Age = 27},
new {Name = "Lily" , Age = 25},
new {Name = "Tom" , Age = 8},
new {Name = "Miley" , Age = 6}};
Console.WriteLine();
Console.WriteLine( "用 foreach 遍历 var 数组" );
foreach ( var person in people)
{
Console.WriteLine(person);
}
Console.WriteLine();
Console.WriteLine( "用 foreach 遍历 var 数组" );
foreach ( var person in people)
{
Console.WriteLine( "Name: {0}, Age: {1}" , person.Name, person.Age);
}
Console.WriteLine();
}
}
} |
复制数组
复制数组有如下方法,但只有最后一种是真正的复制。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
int [] pins = { 1, 2, 3, 4 };
int [] copy = new int [pins.Length];
pins.CopyTo(copy, 0); // 将数组 pins 的内容复制到数组 copy
// CopyTo 是浅拷贝,从指定位置开始复制,复制后两个数组都引用同一组对象 int [] pins = { 1, 2, 3, 4 };
int [] copy = new int [pins.Length];
Array.Copy(pins, copy, copy.Length); // Copy 方法是浅拷贝
int [] pins = { 1, 2, 3, 4 };
int [] copy = ( int [])pins.Clone(); // Clone 方法是浅拷贝
// Clone 放回的是一个 object,所以必须进行类型准换
int [] pins = { 1, 2, 3, 4 };
int [] copy = new int [pins.Length];
for ( int i = 0; i < copy.Length; i++)
{ copy[i] = pins[i]; // 深拷贝,也就是产生了两个数组
} |
维数
数组的维数是没有限制的,可以创建多维数组。
1
2
3
|
string [,] stars = new string [6,8]; // 创建二维数组,6 行 8 列
stars[2, 6] = "Mars" ; // 将单元格(2, 6)的元素设为 Mars
// 使用多维数组时,要准备好捕捉并处理 OutOfMemoryException
|