C#中定义数组–字符串及数组操作
一、一维:
int[] numbers = new int[]{1,2,3,4,5,6}; //不定长
int[] numbers = new int[3]{1,2,3};//定长
二、多维
int[,] numbers = new int[,]{{1,2,3},{1,2,3}}; //不定长
int[,] numbers = new int[2,2]{{1,2},{1,2}}; //定长
三、例子
A:int[] mf1=new int[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];
四、取得数组元素个数:
int b;
b = sizeof (a)/sizeof (*a);
c#字符串及数组操作
2007-12-12 17:53字符串操作(取当前时间)
string time=convert.tostring(DateTime.Today).split( new char []{’ '}); textbox1.text=time[0]; 以空格作为分界点;
数组概述
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] = new byt[4];
}
还可以有更大的数组。例如,可以有三维的矩形