numbers Arry
1. One-dimensional group
namespacearry{classMyarry {/// <summary> ///numbers Arry/// </summary> /// <param name= "args" ></param> Static voidMain (string[] args) { //Sound number datatype[] arryname datatype Specifies the type of element that is stored in the number [] Specifies the dimensions of the number, the rank specifies the size of the number of groups arryname the specified group name int[] n =New int[Ten];//initializing a number of numbers//value n[0] = 111; //Voice number double[] balance = {1,2,3,4}; //Create and initialize the number of int []marks = new int[5] {1,2,3,4,5}//The value of a number changes to another target variable in int []marks =new int[]{1,2,3,4,5}; Int[] score = marks; //in C #, when creating a number, each element is initialized to a default value of 0 (int) 0.0 (float) false (bool) null (Reference)//You can sound a number of variables but not initialize them, but assigning the numbers to this variable must use the new operator intI, J; /*Initialize elements of group N*/ for(i =0; I <Ten; i++) {N[i]= i + -; } /*outputs the value of each element*/ for(j =0; J <Ten; J + +) {Console.WriteLine ("element[{0}] = {1}", J,n[j]); } console.readkey (); } }}
2. Multi-dimensional groups
int[,] Arry2 =New int[,] { {1,2}, {3,4}, {5,6}, {7,8} };//Two dimensions int[,,] Arry3 =New int[2,2,3] { { {1,2,3}, {4,5,6} }, { {7,8,9}, {Ten, One, A} } };//three-dimensional numbersConsole.WriteLine (arry2[0,0]);//output results in 1Console.WriteLine (arry3[1,0,1]);//output results in 8
3. Cross-fault groupThe wrong group is the number of elements, and the dimensions and sizes of the elements can vary.
int[] [] ARR =New int[3][];//To actually instantiate the cross-fault grouparr[0] =New int[] {1,2,3};//valuearr[1] =New int[] {4,5,6,7 }; arr[2] =New int[] {8,9,Ten, One, A }; Console.WriteLine (arr[0][1]);//output results in 2Console.WriteLine (arr[1][1]);//output results in 5
4. Passing numbers to a function
classMyarry2 {DoubleGetaverage (int[] arr,intsize) { inti; Doubleavg; intsum =0; for(i=0; i<size;++i) {sum+=Arr[i]; } avg= (Double) sum/size; returnavg; } /// <summary> ///Pass the numbers to the function, calculate the average/// </summary> /// <param name= "args" ></param> Static voidMain2 (string[] args) {Myarry2 app=NewMyarry2 (); int[] balance =New int[]{1,2,3,4,5};//An int group with 5 elements Doubleavg; /*to pass the number of numbers as a reference*/avg= App.getaverage (Balance,5); Console.WriteLine ("average: {0}", avg); Console.readkey (); } }
5. the number of variablesIf you are unsure of the parameters to pass to the function as a reference, you can use the params key params using the format: public returns the Type method name (params type name [] group name)
classParamArray { Public intAddelements (params int[] arr)// Number of variables { intsum =0; foreach(intIincharr) {Sum+=i; } returnsum; }} ParamArray App=NewParamArray (); intsum = App. Addelements (1,2,3,4); Console.WriteLine ("Total sum is: {0}", sum);
6.arry categoryThe Arry provides a variety of features and methods that are used in the numbers:
- Isfixedsize A value that indicates whether the number has a fixed size
- IsReadOnly A value that indicates whether the number is read only
- Length takes a 32-bit integer, which is the sum of the elements in all the dimensions of the dimension (longlength 64-bit)
- Rank (dimension) for the number of numbers
Common methods:
- Clear is based on the element type, setting the elements of a range in the set as 0,false,null
- Copy (Array,array,int32) starts from the first element of the group and copies a range of elements to the first element position of the other, which is specified by a 32-bit integer
- Copy to (array int32) copies all elements from a single dimension to a specified index location in a specified number of dimensions
- Getlength/getlonglength a 32/64-bit integer representing the total number of elements in the specified dimensions
- Getlowerbound/getupperbound to the lower bound/upper bounds of the specified dimensions in the group
- IndexOf (array,object) search specifies an index that returns the first time an entire group of dimensions
- Reverse (Array) reverse the sequence of elements in a single-dimensional group
- GetValue (Int32)/setvalue (Object,int32) acquire/Set the element setting value for a specified location
- ToString returns a string representing the current image
C # Collection---1. Numbers