当前位置: 首页 > news >正文

发布自己的做家教的网站wordpress取消置顶

发布自己的做家教的网站,wordpress取消置顶,手工制作网站,ui设计最常用的软件一、简述 您可以在数组数据结构中存储相同类型的多个变量。您可以通过指定数组元素的类型来声明数组。如果您希望数组存储任何类型的元素#xff0c;您可以指定object其类型。在 C# 的统一类型系统中#xff0c;所有类型#xff08;预定义的和用户定义的、引用类型和值类型您可以指定object其类型。在 C# 的统一类型系统中所有类型预定义的和用户定义的、引用类型和值类型都直接或间接继承自Object。 type[] arrayName; 数组具有以下属性 1、数组可以是单维、多维或锯齿状的。 2、维数是在声明数组变量时设置的。 3、每个维度的长度是在创建数组实例时确定的。这些值在实例的生命周期内无法更改。 4、交错数组是数组的数组每个成员数组都有默认值null。 5、数组的索引为零包含n元素的数组的索引从0到n-1。 6、数组元素可以是任何类型包括数组类型。 7、数组类型是从抽象基类型Array派生的引用类型。所有数组都实现IList和IEnumerable。您可以使用foreach语句来迭代数组。一维数组还实现IListT和IEnumerableT。  创建数组时可以将数组的元素初始化为已知值。从 C# 12 开始所有集合类型都可以使用Collection 表达式进行初始化。未初始化的元素将设置为默认值。默认值是 0 位模式。所有引用类型包括不可为 null 的类型都具有值null。所有值类型都具有 0 位模式。这意味着NullableT.HasValue属性是false而NullableT.Value属性未定义。在 .NET 实现中该Value属性会引发异常。 二、数组创建示例 // Declare a single-dimensional array of 5 integers. int[] array1 new int[5];// Declare and set array element values. int[] array2 [1, 2, 3, 4, 5, 6];// Declare a two dimensional array. int[,] multiDimensionalArray1 new int[2, 3];// Declare and set array element values. int[,] multiDimensionalArray2 { { 1, 2, 3 }, { 4, 5, 6 } };// Declare a jagged array. int[][] jaggedArray new int[6][];// Set the values of the first array in the jagged array structure. jaggedArray[0] [1, 2, 3, 4]; 1、一维数组 一维数组是相似元素的序列。您可以通过索引访问元素。索引是其在序列中的顺序位置。数组中的第一个元素位于索引处0。您可以使用指定数组元素类型和元素数量的new运算符创建一维数组。以下示例声明并初始化一维数组 int[] array new int[5]; string[] weekDays [Sun, Mon, Tue, Wed, Thu, Fri, Sat];Console.WriteLine(weekDays[0]); Console.WriteLine(weekDays[1]); Console.WriteLine(weekDays[2]); Console.WriteLine(weekDays[3]); Console.WriteLine(weekDays[4]); Console.WriteLine(weekDays[5]); Console.WriteLine(weekDays[6]);/*Output: Sun Mon Tue Wed Thu Fri Sat */ 第一个声明声明了一个由五个整数组成的未初始化数组从array[0]到array[4]。数组的元素被初始化为元素类型的默认值0对于整数。第二个声明声明一个字符串数组并初始化该数组的所有七个值。foreach语句迭代数组的元素weekday并打印所有值。对于一维数组该foreach语句按递增索引顺序处理元素从索引 0 开始以索引 结束Length - 1。 2、传递一维数组作为参数 您可以将初始化的一维数组传递给方法。在以下示例中初始化了一个字符串数组并将其作为参数传递给DisplayArray字符串方法。该方法显示数组的元素。接下来该ChangeArray方法反转数组元素然后该ChangeArrayElements方法修改数组的前三个元素。每个方法返回后该DisplayArray方法都会显示按值传递数组不会阻止对数组元素的更改。 class ArrayExample {static void DisplayArray(string[] arr) Console.WriteLine(string.Join( , arr));// Change the array by reversing its elements.static void ChangeArray(string[] arr) Array.Reverse(arr);static void ChangeArrayElements(string[] arr){// Change the value of the first three array elements.arr[0] Mon;arr[1] Wed;arr[2] Fri;}static void Main(){// Declare and initialize an array.string[] weekDays [Sun, Mon, Tue, Wed, Thu, Fri, Sat];// Display the array elements.DisplayArray(weekDays);Console.WriteLine();// Reverse the array.ChangeArray(weekDays);// Display the array again to verify that it stays reversed.Console.WriteLine(Array weekDays after the call to ChangeArray:);DisplayArray(weekDays);Console.WriteLine();// Assign new values to individual array elements.ChangeArrayElements(weekDays);// Display the array again to verify that it has changed.Console.WriteLine(Array weekDays after the call to ChangeArrayElements:);DisplayArray(weekDays);} } // The example displays the following output: // Sun Mon Tue Wed Thu Fri Sat // // Array weekDays after the call to ChangeArray: // Sat Fri Thu Wed Tue Mon Sun // // Array weekDays after the call to ChangeArrayElements: // Mon Wed Fri Wed Tue Mon Sun 3、多维数组 数组可以有多个维度。例如以下声明创建四个数组两个具有二维两个具有三个维度。前两个声明声明每个维度的长度但不初始化数组的值。后两个声明使用初始值设定项来设置多维数组中每个元素的值。 int[,] array2DDeclaration new int[4, 2];int[,,] array3DDeclaration new int[4, 2, 3];// Two-dimensional array. int[,] array2DInitialization { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // Three-dimensional array. int[,,] array3D new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };// Accessing array elements. System.Console.WriteLine(array2DInitialization[0, 0]); System.Console.WriteLine(array2DInitialization[0, 1]); System.Console.WriteLine(array2DInitialization[1, 0]); System.Console.WriteLine(array2DInitialization[1, 1]);System.Console.WriteLine(array2DInitialization[3, 0]); System.Console.WriteLine(array2DInitialization[3, 1]); // Output: // 1 // 2 // 3 // 4 // 7 // 8System.Console.WriteLine(array3D[1, 0, 1]); System.Console.WriteLine(array3D[1, 1, 2]); // Output: // 8 // 12// Getting the total count of elements or the length of a given dimension. var allLength array3D.Length; var total 1; for (int i 0; i array3D.Rank; i) {total * array3D.GetLength(i); } System.Console.WriteLine(${allLength} equals {total}); // Output: // 12 equals 12 对于多维数组遍历元素时首先递增最右侧维度的索引然后递增下一个左侧维度依此类推直至最左侧索引。以下示例枚举 2D 和 3D 数组 int[,] numbers2D { { 9, 99 }, { 3, 33 }, { 5, 55 } };foreach (int i in numbers2D) {System.Console.Write(${i} ); } // Output: 9 99 3 33 5 55int[,,] array3D new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } }; foreach (int i in array3D) {System.Console.Write(${i} ); } // Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 在二维数组中您可以将左索引视为行将右索引视为列。 但是对于多维数组使用嵌套for循环可以让您更好地控制处理数组元素的顺序 int[,,] array3D new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },{ { 7, 8, 9 }, { 10, 11, 12 } } };for (int i 0; i array3D.GetLength(0); i) {for (int j 0; j array3D.GetLength(1); j){for (int k 0; k array3D.GetLength(2); k){System.Console.Write(${array3D[i, j, k]} );}System.Console.WriteLine();}System.Console.WriteLine(); } // Output (including blank lines): // 1 2 3 // 4 5 6 // // 7 8 9 // 10 11 12 // 4、将多维数组作为参数传递 将初始化的多维数组传递给方法的方式与传递一维数组的方式相同。以下代码显示了 print 方法的部分声明该方法接受二维数组作为其参数。您可以一步初始化并传递一个新数组如以下示例所示。在以下示例中初始化了一个二维整数数组并将其传递给该Print2DArray方法。该方法显示数组的元素。 static void Print2DArray(int[,] arr) {// Display the array elements.for (int i 0; i arr.GetLength(0); i){for (int j 0; j arr.GetLength(1); j){System.Console.WriteLine(Element({0},{1}){2}, i, j, arr[i, j]);}} } static void ExampleUsage() {// Pass the array as an argument.Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }); } /* Output:Element(0,0)1Element(0,1)2Element(1,0)3Element(1,1)4Element(2,0)5Element(2,1)6Element(3,0)7Element(3,1)8 */ 5、交错数组 交错数组是一种其元素为数组且大小可能不同的数组。锯齿状数组有时称为“数组的数组”。它的元素是引用类型并初始化为null. 以下示例演示如何声明、初始化和访问交错数组。 第一个示例jaggedArray是在一条语句中声明的。每个包含的数组都是在后续语句中创建的。 第二个示例jaggedArray2是在一条语句中声明和初始化的。可以混合锯齿状数组和多维数组。 最后一个示例jaggedArray3是一个单维交错数组的声明和初始化该数组包含三个不同大小的二维数组元素。 int[][] jaggedArray new int[3][];jaggedArray[0] [1, 3, 5, 7, 9]; jaggedArray[1] [0, 2, 4, 6]; jaggedArray[2] [11, 22];int[][] jaggedArray2 [[1, 3, 5, 7, 9],[0, 2, 4, 6],[11, 22] ];// Assign 77 to the second element ([1]) of the first array ([0]): jaggedArray2[0][1] 77;// Assign 88 to the second element ([1]) of the third array ([2]): jaggedArray2[2][1] 88;int[][,] jaggedArray3 [new int[,] { {1,3}, {5,7} },new int[,] { {0,2}, {4,6}, {8,10} },new int[,] { {11,22}, {99,88}, {0,9} } ];Console.Write({0}, jaggedArray3[0][1, 0]); Console.WriteLine(jaggedArray3.Length); 交错数组的元素必须先初始化然后才能使用它们。每个元素本身就是一个数组。还可以使用初始值设定项来用值填充数组元素。使用初始值设定项时不需要数组大小。 此示例构建一个数组其元素本身就是数组。每个数组元素都有不同的大小。 // Declare the array of two elements. int[][] arr new int[2][];// Initialize the elements. arr[0] [1, 3, 5, 7, 9]; arr[1] [2, 4, 6, 8];// Display the array elements. for (int i 0; i arr.Length; i) {System.Console.Write(Element({0}): , i);for (int j 0; j arr[i].Length; j){System.Console.Write({0}{1}, arr[i][j], j (arr[i].Length - 1) ? : );}System.Console.WriteLine(); } /* Output:Element(0): 1 3 5 7 9Element(1): 2 4 6 8 */ 6、隐式类型数组 您可以创建一个隐式类型数组其中数组实例的类型是根据数组初始值设定项中指定的元素推断出来的。任何隐式类型变量的规则也适用于隐式类型数组。         以下示例展示了如何创建隐式类型数组 int[] a new[] { 1, 10, 100, 1000 }; // int[]// Accessing array Console.WriteLine(First element: a[0]); Console.WriteLine(Second element: a[1]); Console.WriteLine(Third element: a[2]); Console.WriteLine(Fourth element: a[3]); /* Outputs First element: 1 Second element: 10 Third element: 100 Fourth element: 1000 */var b new[] { hello, null, world }; // string[]// Accessing elements of an array using string.Join method Console.WriteLine(string.Join( , b)); /* Output hello world */// single-dimension jagged array int[][] c [[1,2,3,4],[5,6,7,8] ]; // Looping through the outer array for (int k 0; k c.Length; k) {// Looping through each inner arrayfor (int j 0; j c[k].Length; j){// Accessing each element and printing it to the consoleConsole.WriteLine($Element at c[{k}][{j}] is: {c[k][j]});} } /* Outputs Element at c[0][0] is: 1 Element at c[0][1] is: 2 Element at c[0][2] is: 3 Element at c[0][3] is: 4 Element at c[1][0] is: 5 Element at c[1][1] is: 6 Element at c[1][2] is: 7 Element at c[1][3] is: 8 */// jagged array of strings string[][] d [[Luca, Mads, Luke, Dinesh],[Karen, Suma, Frances] ];// Looping through the outer array int i 0; foreach (var subArray in d) {// Looping through each inner arrayint j 0;foreach (var element in subArray){// Accessing each element and printing it to the consoleConsole.WriteLine($Element at d[{i}][{j}] is: {element});j;}i; } /* Outputs Element at d[0][0] is: Luca Element at d[0][1] is: Mads Element at d[0][2] is: Luke Element at d[0][3] is: Dinesh Element at d[1][0] is: Karen Element at d[1][1] is: Suma Element at d[1][2] is: Frances */ 在前面的示例中请注意对于隐式类型数组初始化语句的左侧没有使用方括号。此外交错数组的初始化方式与new []一维数组类似。 当您创建包含数组的匿名类型时该数组必须在该类型的对象初始值设定项中隐式类型化。在以下示例中contacts是一个匿名类型的隐式类型数组每个匿名类型都包含一个名为 的数组PhoneNumbers。该var关键字不在对象初始值设定项内部使用。 var contacts new[] {new{Name Eugene Zabokritski,PhoneNumbers new[] { 206-555-0108, 425-555-0001 }},new{Name Hanying Feng,PhoneNumbers new[] { 650-555-0199 }} };
http://www.pierceye.com/news/307000/

相关文章:

  • 西安知名的集团门户网站建设服务商潍坊网站开发asp培训
  • 网站服务器连接被重置为什么高德地图没有外国位置信息
  • 帝国cms 仿站 wordpress天津新亚太工程建设监理有限公司网站
  • 精品网站导航 做最好的导航网站建设数据库选择
  • 蓝杉网站建设公司贵阳网站建设公司排名
  • 苏州专业高端网站建设机构建网站公司下载快手
  • 中堂仿做网站个人网站设计论文道客巴巴
  • 怎么用ps做网站效果图24什么网站建设
  • 网站开发技术有网站建设方案 pdf
  • 网站建设教程浩森宇特福州医院网站建设公司
  • 怎样在网站上做超链接网站商城是用什么框架做的
  • 网站建设增城wordpress新文章类型
  • 广州市招投标中心官网上海网站关键词优化
  • 很多网站开发没有框架如何制作的长沙旅游景点大全排名
  • 云南网站推广的目的做动画的网站有哪些
  • 网站建设公司在哪里找资源模拟建设网站
  • 如何盗用网站模板哈尔滨公告
  • 管理咨询网站焦作专业做网站公司哪家好
  • 在国内做跨境电商怎么上外国网站网站不收录
  • 网站介绍ppt怎么做屏蔽网站ip
  • it公论 是建立在什么网站wordpress搬迁数据库连接失败
  • 南县建设局网站营销型网站开发流程包括
  • 有关应用网站申请免费网站空间
  • 二手书交易网站开发现状营销型网站建设推荐乐云seo
  • 山西网站建设怎么样seo优化网站多少钱
  • 网站建设设计模板磁力链最佳的搜索引擎
  • 单位外部网站建设价格哪些网站可以做视频直播
  • 广州黄埔网站建设公司国外做调灵风暴的网站
  • 珠海附近交友平台软件广州网站优化推广方案
  • cgi做网站如何将网站加入百度图 推广