c#多维数组的建立及操作 总结

来源:互联网 发布:手机直播网络要求 编辑:程序博客网 时间:2024/06/09 14:52

1C#如何定义和使用多维数组

不建议使用ArrayList,当数组里的元素是值类型在操作的时候会出现大量的装箱与拆箱步骤性能会损失许多,而是应该用什么类型的元素创建什么类型的数组,除非你的元素有交叉或不确定才考虑采用ArrayList。


多维数组定义如下:
数组可以具有多个维度。例如,下列声明创建一个四行两列的二维数组:
C#

int[,]array = new int[4, 2];

另外,下列声明创建一个三维(4、2 和 3)数组:
C#

int[,,] array1 = new int[4, 2, 3];

数组初始化
可以在声明数组时将其初始化,如下例所示:
C#

int[,]array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } } };
也可以初始化数组但不指定级别:
C#

int[,]array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
如果选择声明一个数组变量但不将其初始化,必须使用 new 运算符将一个数组分配给此变量。例如:
C#

int[,]array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error
也可以给数组元素赋值,例如:
C#

array5[2,1] = 25;


写一个详细的小例子,用一个动态的STRING型数组存放一个TABLE中的每一个单元中的内容:

     DataTabledt = ds.Tables[0];//取出一个内容是动态的表
     string[,] str = newstring[dt.Rows.Count,dt.Columns.Count];

    //用数组str来存放一个TABLE中的每一个单元中的内容

    //dt.Rows.Count是表的行数,dt.Columns.Count是表的例数
    for(int i =0;i < dt.Rows.Count ; i++)
    {
       for(int j=0;j<dt.Columns.Count;j++)
       {
          str[i,j] =dt.Rows[i][j].ToString().Trim();
       }
    }

2 定义不定长数组

一维:

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

int[] numbers = new int[3]{1,2,3};//定长

多维

int[,] numbers = newint[,]{{1,2,3},{1,2,3}}; //不定长

int[,] numbers = new int[2,2]{{1,2},{1,2}};//定长

实例:

A:int[] mf1=newint[6];

       //注意初始化数组的范围,或者指定初值; //包含6个元素的一维整数数组,初值1,2,3,4,5,6

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

B://一维字符串数组,如果提供了初始值设定项,则还可以省略 new 运算符

      string[] mf3={"c","c++","c#"};

C://一维对象数组

      Object[] mf4 = new Object[5] { 26, 27, 28, 29, 30 };

D://二维整数数组,初值mf5[0,0]=1,mf5[0,1]=2,mf5[1,0]=3,mf5[1,1]=4

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

E://6*6的二维整型数组

       int[,]mf6=new mf[6,6];


3 实例定义一维和二维数组


[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace ConsoleApplication1  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             //一维数组定义与初始化  
  13.             int[] one1 = new int[] {3,2,1 };//第一种方式  
  14.             int[] one2 = { 3, 2, 1 };      //第二种方式  
  15.             int[] one3;                    //第三种方式  
  16.             one3=new int[]{3,2,1};  
  17.   
  18.             
  19.             //二维数组定义与初始化  
  20.   
  21.             //不规则二维数组  
  22.             int[][] array = new int[2][];  
  23.             array[0] = new int[3];  
  24.             array[0][1] = 11;  
  25.             array[0][2] = 12;  
  26.             array[1] = new int[] { 1, 2, 3, 4,5 };  
  27.   
  28.             //或int[][] array = new int[2][] { new int[] {0,11,12 }, new int[] {1,2,3,4,5 }};  
  29.             
  30.             Console.WriteLine( "不规则二维数组:  ");  
  31.             for (int i = 0; i < array.Length; i++)  
  32.             {  
  33.                 foreach (int j in array[i])  
  34.                 {  
  35.                     Console.Write(j+"   ");  
  36.                 }  
  37.                 Console.WriteLine();  
  38.             }  
  39.   
  40.             //固定长度的矩阵数组  
  41.             int[,] arrray1=new int[2,5]{{9,9,9,9,0},{0,0,9,0,0}};  
  42.             Console.WriteLine("规则二维数组输出方法一:  ");  
  43.             for (int ii = 0; ii < 2; ii++)                        //输出方法一  
  44.             {  
  45.                 for (int j = 0; j < 5; j++)  
  46.                 {  
  47.                     Console.Write(arrray1[ii,j] + "   ");  
  48.                 }  
  49.                 Console.WriteLine();  
  50.             }  
  51.             Console.WriteLine("规则二维数组输出方法二:  ");  
  52.             foreach (int j in arrray1)//arrray.length=10;      //输出方法二  
  53.             {  
  54.                 Console.Write(j + "   ");  
  55.             }  
  56.             Console.WriteLine();  
  57.             Console.ReadLine();  
  58.         }  
  59.     }  
  60. }  

4 对数组操作

取得数组元素个数:

        int b;   

       b = sizeof (a)/sizeof (*a);    

c#字符串及数组操作

2007-12-12 17:53字符串操作(取当前时间)

stringtime=convert.tostring(DateTime.Today).split( new char []{''});     textbox1.text=time[0]; 以空格作为分界点;

 

1)数组概述

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

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

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

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

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

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

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

2)声明数组

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] = new byt[4];

}

还可以有更大的数组。例如,可以有三维的矩形数组:int[,,] buttons = new int[4,5,3];

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

3)初始化数组

C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。下面的示例展示初始化不同类型的数组的各种方法。

注意如果在声明时没有初始化数组,则数组成员将自动初始化为该数组类型的默认初始值。另外,如果将数组声明为某类型的字段,则当实例化该类型时它将被设置为默认值 null

一维数组

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","Albert"} };

 

如果提供了初始值设定项,则还可以省略 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}, newint[] {5,6,7,8,9} };

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

 

4)访问数组成员

访问数组成员可以直接进行,类似于在 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;

 

5)数组是对象

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

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

int LengthOfNumbers = numbers.Length;

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

 

6)对数组使用 foreach

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

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

foreach (int i innumbers){System.Console.WriteLine(i);}

由于有了多维数组,可以使用相同方法来循环访问元素,例如:

int[,] numbers = new int[3, 2] {{9, 99},{3, 33}, {5, 55}};

foreach(int i innumbers){Console.Write("{0} ", i);}

该示例的输出为: 9 99 3 33 5 55

不过,由于有了多维数组,使用嵌套 for 循环将使您可以更好地控制数组元素

0 0
原创粉丝点击