您的位置:首页 > 编程语言 > C#

C#中结构数组怎么定义?怎么获取一个字符的ASCII码?

2011-10-09 15:50 363 查看
数组概述

C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似,但还有一些差异应引起注意。

声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。

int[] table; // not int table[];

另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。

int[] numbers; // declare numbers as an int array of any size

numbers = new int[10]; //numbers is a 10-element array

numbers = new int[20]; //now it's a 20-element array

声明数组

C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:

一维数组:

int[] numbers;

多维数组:

string[,] names;

数组的数组(交错的):

byte[][] scores;

声明数组(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:

一维数组:

int[] numbers = new int[5];

多维数组:

string[,] names = new string[5,4];

数组的数组(交错的):

byte[][] scores = new byte[5][];

for (int x = 0; x < scores.Length; x++)

{

scores[x] = newbyte[4];

}

还可以有更大的数组。例如,可以有三维的矩形数组

int[,,] buttons = new int[4,5,3];

甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组。

int[][,,][,] numbers;

示例:

下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。

// arrays.cs

using System;

class DeclareArraysSample

{

public static voidMain()

{

//Single-dimensional array

int[] numbers =new int[5];

//Multidimensional array

string[,] names =new string[5,4];

// Array-of-arrays(jagged array)

byte[][] scores =new byte[5][];

// Create thejagged array

for (int i = 0; i< scores.Length; i++)

{

scores[i] =new byte[i+3];

}

// Print length ofeach row

for (int i = 0; i< scores.Length; i++)

{

Console.WriteLine("Length of row {0} is{1}", i, scores[i].Length);

}

}

}

输出

Length of row 0 is 3

Length of row 1 is 4

Length of row 2 is 5

Length of row 3 is 6

Length of row 4 is 7

初始化数组

C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。特别要注意的是,如果声明时未初始化数组,则数组成员自动初始化为该数组类型的默认初始值。

下面的示例展示初始化不同类型的数组的各种方法。

一维数组

int[] numbers = new int[5] {1, 2, 3, 4, 5};

string[] names = new string[3] {"Matt","Joanne", "Robert"};

可省略数组的大小,如下所示:

int[] numbers = new int[] {1, 2, 3, 4, 5};

string[] names = new string[] {"Matt","Joanne", "Robert"};

如果提供了初始值设定项,还可省略 new 语句,如下所示:

int[] numbers = {1, 2, 3, 4, 5};

string[] names = {"Matt", "Joanne","Robert"};

多维数组

int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = new string[2, 2] {{"Mike","Amy"}, {"Mary","Albert"} };

可省略数组的大小,如下所示:

int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = new string[,] {{"Mike","Amy"}, {"Mary","Ray"} };

如果提供了初始值设定项,还可省略 new 语句,如下所示:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };

string[,] siblings = { {"Mike", "Amy"},{"Mary", "Albert"} };

交错的数组(数组的数组)

可以像下例所示那样初始化交错的数组:

int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[]{5,6,7,8,9} };

可省略第一个数组的大小,如下所示:

int[][] numbers = new int[][] { new int[] {2,3,4}, new int[]{5,6,7,8,9} };

— 或 —

int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

请注意,对于交错数组的元素没有初始化语法。

访问数组成员

访问数组成员可以直接进行,类似于在 C/C++ 中访问数组成员。例如,下面的代码创建一个名为 numbers 的数组,然后向该数组的第五个元素赋以 5:

int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

numbers[4] = 5;

下面的代码声明一个多维数组,并向位于 [1, 1] 的成员赋以 5:

int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };

numbers[1, 1] = 5;

下面声明一个一维交错数组,它包含两个元素。第一个元素是两个整数的数组,第二个元素是三个整数的数组:

int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3,4, 5}};

下面的语句向第一个数组的第一个元素赋以 58,向第二个数组的第二个元素赋以 667:

numbers[0][0] = 58;

numbers[1][1] = 667;

数组是对象

在 C# 中,数组实际上是对象。System.Array是所有数组类型的抽象基类型。可以使用System.Array 具有的属性以及其他类成员。这种用法的一个示例是使用“长度”(Length) 属性获取数组的长度。下面的代码将 numbers 数组的长度(为 5)赋给名为LengthOfNumbers 的变量:

int[] numbers = {1, 2, 3, 4, 5};

int LengthOfNumbers = numbers.Length;

System.Array 类提供许多有用的其他方法/属性,如用于排序、搜索和复制数组的方法。

对数组使用 foreach

C# 还提供 foreach 语句。该语句提供一种简单、明了的方法来循环访问数组的元素。例如,下面的代码创建一个名为 numbers 的数组,并用 foreach 语句循环访问该数组:

int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};

foreach (int i in numbers)

{

System.Console.WriteLine(i);

}

下列代码示例使用 ASCIIEncoding.GetBytes 方法将 Unicode 字符串转换为字节数组。数组中每个字节表示字符串中该位置字母的 ASCII 值。

[Visual Basic]

Dim MyString As String = "Encoding String."

Dim AE As New ASCIIEncoding()

Dim ByteArray As Byte() = AE.GetBytes(MyString)

Dim x as Integer

For x = 0 To ByteArray.Length - 1

Console.Write("{0}", ByteArray(x))

Next

[C#]

string MyString = "Encoding String.";

ASCIIEncoding AE = new ASCIIEncoding();

byte[] ByteArray = AE.GetBytes(MyString);

for (int x = 0;x <= ByteArray.Length - 1; x++)

{

Console.Write("{0}", ByteArray[x]);

}

此示例将下列内容显示到控制台。字节 69 是字符 E 的 ASCII 值,字节 110 是字符 n 的 ASCII 值,等等。

69 110 99 111 100 105 110 103 32 83 116 114 105 110 103 46

下列代码示例使用 ASCIIEncoding 类将前面的字节数组转换为字符数组。GetChars 方法用来解码字节数组。

[VisualBasic]

Dim AE As New ASCIIEncoding()

Dim ByteArray As Byte() = { 69, 110, 99, 111, 100, 105, 110,103, 32, 83, 116, 114, 105, 110, 103, 46 }

Dim CharArray As Char() = AE.GetChars(ByteArray)

Dim x As Integer

For x = 0 To CharArray.Length - 1

Console.Write(CharArray(x))

Next

[C#]

ASCIIEncoding AE = new ASCIIEncoding();

byte[] ByteArray = { 69, 110, 99, 111, 100, 105, 110, 103, 32,83, 116, 114, 105, 110, 103, 46 };

char[] CharArray = AE.GetChars(ByteArray);

for (int x = 0;x <= CharArray.Length - 1; x++)

{

Console.Write(CharArray[x]);

}

以上代码将 Encoding String. 文本显示到控制台
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: