Introduction to data types in C # (array) array definitions
Arrays can be treated as one or more sets of data of the same data type, including one-dimensional arrays, multidimensional arrays, and jagged arrays
Data statement
Several declarations and instantiations of one-dimensional arrays
type[] TypeName = new Type[n];//define an array but not assign a value
Type[0] = item1;
TYPE[1] = item2;
TYPE[2] = Item3; ......
Type[n-1] =itemn;
type[] TypeName = new Type[n] {item1,item2,item3,...... itemn};//system can automatically calculate the length of the array
type[] TypeName = new type[] {item1,item2,item3,...... itemn};//system can automatically calculate the length of the array
Type[] Typenmae = {item1,item2,item3,...... itemn};//Omit assigned data type, system can automatically infer, very concise definition way
classProgram {Static voidMain (string[] args) { int[] IntA =New int[5]; inta[0] =1; inta[1] =2; inta[2] =3; inta[3] =4; inta[4] =5; int[] IntB =New int[] {1,2,3,4,5 }; int[] IntC =New int[5] {1,2,3,4,5 }; int[] Intd = {1,2,3,4,5 }; //The value of the array, returns the index bit 3, and returns a value of 3 intItem3 = inta[2]; //int[] Abstract base class System.Array, which inherits the IEnumerable interface, can use the foreach foreach(intIteminchIntA) Console.WriteLine ("Item Is:{0}", item); //array members can be accessed using a for loop for(inti =0; i < inta.length; i++) Console.WriteLine ("Each is : {0}", Inta[i]); //indicates that an array member can be assigned a value BOOLB1 = inta.isreadonly;//returns false BOOLb2 = inta.issynchronized;//returns FALSE, which can be accessed asynchronously//once the array is defined, it cannot be added, inserted, deleted BOOLB3 = inta.isfixedsize;//returns true to indicate that the array is a fixed-length//Multidimensional Array Declaration string[,] stra =New string[3,2]{ {"GZ","SZ"}, {"CD","DZ"}, {"CS","ZZ" } }; string[,] StrB =New string[,] { {"GZ","SZ"}, {"CD","DZ"}, {"CS","ZZ" } }; string[,] strc = {{"GZ","SZ"}, {"CD","DZ"}, {"CS","ZZ" } }; string[,] StrD =New string[3,2]; strd[0,0] ="GZ"; strd[0,1] ="SZ"; //values for multidimensional arrays stringITEM11 = stra[1,1]; stringItemX = strd[2,1];//initialization is not assigned a value of NULL//multidimensional array member foreach traversal foreach(stringIteminchstra) Console.WriteLine ("foreach traversal: {0}", item); //for traversal of multi-dimension members intW1 = Stra.getupperbound (1)+1; intW2 = Stra.getupperbound (0)+1; for(inti =0; I < W2; i++) for(intj =0; J < W1; J + +) Console.WriteLine ("For traversal: {0}", Stra[i, j]); //a jagged array that represents the array of members as arrays int[] Arry =New int[2][]; arry[0] =New int[3] {6,7,8 }; arry[1] =New int[4] {9,4,5,6 }; foreach(varIteminchArry)foreach(intElementinchItem) Console.WriteLine (Element); } }Array abstract base class System.Array
System.Array is an abstract class of specific arrays, with specific arrays inheriting from the System.Array class
Static voidMain (string[] args) { //creating an array instance using the System.Array static method CreateInstanceArray Strarray = array.createinstance (typeof(string),4); Strarray.setvalue ("Beijing",0); Strarray.setvalue ("Shanghai",1); Strarray.setvalue ("Tianjin",2); Strarray.setvalue ("Chongqin",3); //the above notation is equivalent toArray strArray1 =New string[4]{"Beijing","Shanghai","Tianjin","Chongqin"}; //foreach traversal array foreach(varIteminchStrarray) Console.WriteLine ("foreach one-dimensional array traversal list:{0}", item); //For traversal Arrays for(inti =0; i < strarray.length; i++) Console.WriteLine ("For one-dimensional array traversal list:{0}", Strarray.getvalue (i)); //invert one-dimensional array strarrayArray.reverse (Strarray); //Array to create a multidimensional arrayArray strArray2 = array.createinstance (typeof(int),New int[] {3,2 }); Strarray2.setvalue (Ten,New int[]{0,0}); Strarray2.setvalue ( One,New int[] {0,1 }); Strarray2.setvalue ( A,New int[] {1,0 }); Strarray2.setvalue ( -,New int[] {1,1 }); Strarray2.setvalue ( -,New int[] {2,0 }); Strarray2.setvalue ( the,New int[] {2,1 }); //the multidimensional array definition above is equivalent to int[,] strArray3 =New int[3,2] { {Ten, One}, { A, -}, { -, the } }; foreach(varIteminchstrArray2) Console.WriteLine ("foreach Multidimensional array traversal: {0}", item); //the 0 dimension has a length of 3, the upper limit is 2, the lower limit is 0, and the running result shows Ok,ok if(Strarray2.getlength (0) = = Strarray2.getupperbound (0)-Strarray2.getlowerbound (0) +1) Console.WriteLine ("Ok,ok"); }
Introduction to data types in C # (arrays)