In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to understand C#array, the content is concise and easy to understand, absolutely can make your eyes shine, through the detailed introduction of this article I hope you can gain something.
Foreword:
If you need to use multiple objects of the same type, you can use arrays and collections. C#uses special notation to declare, initialize, and use arrays. The Array class works in the background, providing several methods for sorting and filtering elements in arrays. Using enumerators, you can iterate over all the elements in an array.
If you need to use multiple objects of different types, you can use the tuple type.
I. One-dimensional array of simple arrays
An array is a data structure that can contain multiple elements of the same type.
1. Declarations of arrays
When declaring an array, you define the type of the elements in the array, followed by a pair of empty square brackets and a variable name.
int[] myArray;2. Initialization of array
Once an array is declared, memory must be allocated for the array to hold all elements of the array. Array is a reference type, so it must be allocated memory on the heap. To do this, initialize the variables of the array by specifying the type and number of elements in the array using the new operator.
myArray = new int[4];
After declaring and initializing the array, the variable myArray references four integer values on the managed heap:
在指定了数组的大小后,就不能重新设置数组的大小。如果事先不知道数组中应包含多少个元素,就可以使用集合。
除了在两个语句中声明和初始化数组之外,还可以在一个语句中声明和初始化数组:
int[] myArray = new int[4];
还可以使用数组初始化器为数组的每个元素复制。数组初始化器只能在声明数组变量时使用,不能在声明数组之后使用。
int[] myArray = new int[4]{1,3,5,7};
如果用花括号初始化数组,可以不指定数组的大小,因为编译器会自动统计元素的个数:
int[] myArray = new int[]{1,3,5,7};
也可以使用更简单的形式:
int[] myArray = {1,3,5,7};3.访问数组元素
在声明和初始化数组之后,就可以使用索引器访问其中的元素了。数组只支持有整型参数的索引器。
索引器总是以0开头,表示第一个元素。可以传递给索引器的最大值是元素个数减1,因为索引从0开始:
int[] myArray = {1,3,5,7}; int v1 = myArray[0]; int v2 = myArray[1]; myArray[3] = 4;
可以使用数组的Length属性获取元素的个数。
4.数组中使用引用类型
数组除了能声明预定义类型的数组,还可以声明自定义类型的数组。
public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return String.Format("{0} {1}", FirstName, LastName); } } Person[] myPersons = new Person[2]; myPersons[0] = new Person { FirstName = "Ayrton", LastName = "Senna" }; myPersons[1] = new Person { FirstName = "Michael", LastName = "Schumacher" };
如果数组中的元素是引用类型,就必须为每个数组元素分配内存。如果使用了数组中未分配内存的元素,就会抛出NullReferenceException类型的异常。
下面是内存情况:
对自定义类型也可以使用数组初始化器:
Person[] myPersons2 = { new Person { FirstName="Ayrton", LastName="Senna"}, new Person { FirstName="Michael", LastName="Schumacher"} };二.多维数组
多维数组用两个或多个整数来索引。
在C#中声明多维数组,需要在方括号中加上逗号。数组在初始化时应指定每一维的大小(也称为阶)。
int[,] twoDim = new int[3,3]; twoDim[0,0] = 1; twoDim[0,1] = 2; twoDim[0,2] = 3; twoDim[1,0] = 4; twoDim[1,1] = 5; twoDim[1,2] = 6; twoDim[2,0] = 7; twoDim[2,1] = 8; twoDim[2,2] = 9;
声明数组之后,就不能修改其阶数了。
也可以使用初始化器来初始化多维数组:
int[,] twoDim ={ {1,2,3}, {4,5,6}, {7,8,9} };
使用数组初始化器时,必须初始化数组的每个元素,不能遗漏任何元素。
声明一个三位数组:
int[,,] threeDim ={ {{1,2},{3,4}}, {{5,6},{7,8}}, {{9,10},{11,12}} }; Console.WriteLine(threeDim[0,1,1]);三.锯齿数组
二维数组的大小对应于一个矩形,而锯齿数组的大小设置比较灵活,在锯齿数组中,每一行都可以有不同的大小。
在声明锯齿数组时,要依次放置左右括号。在初始化锯齿数组时,只在第一对方括号中设置该数组包含的行数。定义各行中元素个数的第二个方括号设置为空,因为这类数组的每一行包含不同的元素个数。之后,为每一行指定行中的元素个数:
int[][] jagged = new int[3][]; jagged[0] = new int[2]{1,2}; jagged[1] = new int[4]{3,4,5,6}; jagged[2] = new int[3]{7,8};
迭代锯齿数组中的所有元素的代码可以放在嵌套的for循环中。在外层的for循环中迭代每一行,在内层的for循环中迭代一行中的每个元素:
for(int row = 0;row
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.