1. Interleaved array
One-dimensional array whose data type is one-dimensional array
int[][] intsArray = new int[2][] { new int[5] { 1, 2, 3, 4, 5 }, new int[4] { 1, 2, 3, 4 } } ; string[] stringArray1 = { "1", "2" }; string[] stringArray2 = { "3", "2" }; string[][] stringsArray = new string[2][] { stringArray1,stringArray2 };
2.forEach
Iterate over the values in the output array:
foreach (int[] item in intsArray) { foreach (int item1 in item) { Console.WriteLine(item1); } }
3. Two-dimensional array:
int[,][] intssArray = new int[2, 2][] { { new int[2] { 1, 33 }, new int[3] { 1,2, 4} },{ new int [2] { 7, 9}, new int[3] { 6, 4, 3 } } }; Console.WriteLine(intssArray[0,1][2]);
4.Array array
Introduction: It is the base class of all arrays in C#, it is in System
//Definition in the namespace provides various properties and methods for arrays
Create a one-dimensional array:
//Create a one-dimensional array int[] intArray = { 1, 2,} //array represents the object of Array and also represents a one-dimensional array
Use CreateInstance to create a one-dimensional array:
Array array = Array.CreateInstance(typeof(int), 3); Console.WriteLine(typeof(int)); //Store 100 at the position with index value 0 array.SetValue(100, 0); array.SetValue(200, 1); array.SetValue(300, 2); // Console.WriteLine(array.GetValue(1));
Use CreateInstance to create a two-dimensional array:
Array intsArray = Array.CreateInstance(typeof(int), 2, 2);//Create a two-dimensional array //Set the value of the two-dimensional array intsArray.SetValue(100, 0,0); intsArray.SetValue(200, 0,1); intsArray.SetValue(300, 1, 0); intsArray.SetValue(400, 1, 1); //Get value Console.WriteLine(intsArray.GetValue(0,0)); Console.WriteLine(intsArray.GetValue(1,1));
5.Array API
Indexof(Array array, Object) Returns the first occurrence of the index. If not found, returns -1 Sort(Array array) Sort from small to large (only supports one-dimensional arrays) Do not generate new array objects Sort the elements of old array objects Reverse(Array array) Array reverse Clear(Array array, int index, int length) sets all elements in a range to initial values Copy deep copies the contents of an array to another array
Indexof(Array array, Obejct) returns the first occurrence of the subscript. If not found, returns -1.
Parameter 1 Array object to be searched Element to be searched
int indexOfNumber = Array.IndexOf(intArray1, 2); Console.WriteLine(indexOfNumber); int indexOfNumber1 = Array.IndexOf(array1, 1); Console.WriteLine(indexOfNumber1);
Sort:
Array.Sort(intArray1); foreach (int i in intArray1) { Console.WriteLine(i); }
Reverse array inversion:
Array.Reverse(array1); foreach (var item in array1) { Console.WriteLine(item); }
Clear(Array array, int index, int length) sets all elements in a range to their initial values:
Array.Clear(intArray1, 1, 3); foreach (var item in intArray1) { Console.WriteLine(item); }
Shallow copy: = ; After copying with this method, the two arrays share a memory 1000 2 3 4
Deep copy: The new array does not have the same value as the old array
//copy copy Array array2 = Array.CreateInstance(typeof(int), 4); for (int i = 0;i < array2.Length;i + + ) { array2.SetValue(i + 1, i); } Array array3 = Array.CreateInstance(typeof(int), 4);; //array3 = array2; //shallow copy Array.Copy(array2, array3, 4); //Deep copy foreach (int i in array3) { Console.WriteLine(i); } array3.SetValue(1000, 0); foreach (int i in array3) { Console.WriteLine(i); } //1000 2 3 4 foreach (int i in array2) { Console.WriteLine(i); } // 1 2 3 4 /*If shallow copy = assignment is used, the two arrays share a memory 1000 2 3 4*
6. Variable parameters params
1.params is the abbreviation of ParamArrayAttribute (parameter array attribute)
2.param solves the problem of passing indefinite parameters in C#
3. The params parameter must be defined at the end of the parameter list.
4.params must be a one-dimensional array
5.params can only define a one-dimensional array in one method
In C#, the params
keyword is used to declare a variable parameter, which means that you can pass an unlimited number of parameters to the method, and these parameters will be encapsulated into an array for processing inside the method. . Variadics are often used to simplify method calls, especially when your method can accept a different number of arguments.
using System; class Program { static void Main(string[] args) { int sum1 = Sum(1, 2, 3, 4, 5); int sum2 = Sum(10, 20, 30); Console.WriteLine("Sum1: " + sum1); Console.WriteLine("Sum2: " + sum2); } static int Sum(params int[] numbers) { int sum = 0; foreach (int num in numbers) { sum + = num; } return sum; } }
In the above example, the Sum
method uses the params
keyword to accept a variable number of integer parameters. You can pass any number of integer values to this method and it will store them in an integer array and then calculate their sum.
Note tips:
- The
params
keyword can only be used as the last parameter of a method, and there can only be oneparams
parameter in the same method. - You can call the method without passing parameters, which will create an empty array.
- If the method has other parameters, you need to pass the other parameters first before passing the
params
parameter when calling, or use named parameters to clearly specify the meaning of the parameters. , - Example:
-
ProgramMothod(10,20); ProgramMothod2(30, 20); int[] intArray1 = new int[3] {3,4,5}; ProgramMothod2(50, 60, intArray1); ProgramMothod2(10, 20, 30, 40, 50); Console.ReadKey(); } public static void ProgramMothod(int a ,int b) { Console.WriteLine(a + b); } public static void ProgramMothod1(int a, int b,int c) { Console.WriteLine(a + b + c); } //Add and sum any number of integers using only one method. public static void ProgramMothod2(int a, int b,params int[]intArray) { int tempC = a + b; if (intArray != null) { foreach (var item in intArray) { tempC + = item; } Console.WriteLine(tempC); } else{ Console.WriteLine(tempC); } }
Use the
params
keyword to handle a variable number of parameters.
The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Algorithm skill tree Home page Overview 56997 people are learning the system