C#数组

来源:互联网 发布:淘宝专卖店是正品吗 编辑:程序博客网 时间:2024/05/16 05:04

数组

数组是一组通过数字索引访问的的数据项。准确来说,数据是一组相同类型的数据点(如:int数组,string数组,Car数组)。

int[]  vInts=new int[3];//包含2个整型的数据,编号0~2

string[] vStrings=new String[2];//包含2个字符串的数字,编号0、1

数组声明中的数字就表示像的总数,而不是上界。数组的下界总是从0开始。
可以使用索引来填充元素了。

vInts[0]=1;vInts[1]=2;vInts[2]=3;

vStrings[0]="bruce";vStrings[0]="rita";

除了逐个元素填充数组之外,还可以使用数组初始化语法来填充数组元素通过花括号{}内指定每一个数据项来实现。如:

int[] vInts1=new int[3]{0,1,2};

int[] vInts2=new int[]{0,1,2};

int[] vInts3={0,1,2};

当使用花括号初始化数据时,可以不指定数组的大小(vInts2的方法)。因为可以从花括号的项的个数推断。new关键字也是可选的(vInts3的方法)。

System.Array基类 

Array 类是支持数组的语言实现的基类。数组从Array基类中获取了许多功能,使用Array基类的公共成员,我们能使用统一的对象模型操作数组。

部分公共属性:

Rank 获取 Array 的秩(维数)。

Length 获得一个 32 位整数,该整数表示 Array 的所有维数中元素的总数。

部分公共方法

Clear 将 Array 中的一系列元素设置为零、false 或 空引用(在 Visual Basic 中为 Nothing),具体取决于元素类型。 

Clone 创建 Array 的浅表副本。 

Copy 将一个 Array 的一部分元素复制到另一个 Array 中,并根据需要执行类型强制转换和装箱。 

CopyTo 将当前一维 Array 的所有元素复制到指定的一维 Array 中。 

Exists 确定指定数组包含的元素是否与指定谓词定义的条件匹配。

Find 搜索与指定谓词定义的条件匹配的元素,然后返回整个 Array 中的第一个匹配项。

FindAll 检索与指定谓词定义的条件匹配的所有元素。 

Reverse 反转一维 Array 或部分 Array 中元素的顺序。

Resize 将数组的大小更改为指定的新大小。 

Sort  对一维 Array 对象中的元素进行排序。 

GetEnumerator 返回 Array 的 IEnumerator。  

GetUpperBound 获取 Array 的指定维度的上限。 

GetLowerBound 获取 Array 中指定维度的下限。 

代码示例说明 Array.Copy 如何在 integer 类型的数组和 Object 类型的数组之间复制元素。

复制代码
public class SamplesArray {

public static void Main() {

// Creates and initializes a new integer array and a new Object array.
int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
Object[] myObjArray
= new Object[5] { 26, 27, 28, 29, 30 };

// Prints the initial values of both arrays.
Console.WriteLine( "Initially," );
Console.Write(
"integer array:" );
PrintValues( myIntArray );
Console.Write(
"Object array: " );
PrintValues( myObjArray );

// Copies the first two elements from the integer array to the Object array.
Array.Copy( myIntArray, myObjArray, 2 );

// Prints the values of the modified arrays.
Console.WriteLine( "\nAfter copying the first two elements of the integer array to the Object array," );
Console.Write(
"integer array:" );
PrintValues( myIntArray );
Console.Write(
"Object array: " );
PrintValues( myObjArray );

// Copies the last two elements from the Object array to the integer array.
Array.Copy( myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2 );

// Prints the values of the modified arrays.
Console.WriteLine( "\nAfter copying the last two elements of the Object array to the integer array," );
Console.Write(
"integer array:" );
PrintValues( myIntArray );
Console.Write(
"Object array: " );
PrintValues( myObjArray );
}


public static void PrintValues( Object[] myArr ) {
foreach ( Object i in myArr ) {
Console.Write(
"\t{0}", i );
}
Console.WriteLine();
}

public static void PrintValues( int[] myArr ) {
foreach ( int i in myArr ) {
Console.Write(
"\t{0}", i );
}
Console.WriteLine();
}
}
/*
This code produces the following output.

Initially,
integer array: 1 2 3 4 5
Object array: 26 27 28 29 30

After copying the first two elements of the integer array to the Object array,
integer array: 1 2 3 4 5
Object array: 1 2 28 29 30

After copying the last two elements of the Object array to the integer array,
integer array: 1 2 3 29 30
Object array: 1 2 28 29 30
*/
复制代码
原创粉丝点击