Jagged Array in C# - C# Corner
文章推薦指數: 80 %
The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays." A special type ... JaggedArrayinC# C#Curator UpdateddateMar07,2020 433.2k 0 4 AC#jaggedarrayisanarraywhoseelementsarearrays.Inthisarticle,IwillexplainjaggedarrayinC#andhowtoimplementjaggedarraysinC#. facebook twitter linkedIn Reddit WhatsApp Email Bookmark Print OtherArtcile Expand Inthisarticle,IwillexplainaboutjaggedarrayinC#. WhatisajaggedarraysinC# AjaggedarrayinC#isanarraywhoseelementsarearrays.Theelementsofajaggedarraycanbeofdifferentdimensionsandsizes.Ajaggedarrayissometimescalledan"arrayofarrays."AspecialtypeofarrayisintroducedinC#.AJaggedArrayisanarrayofanarrayinwhichthelengthofeacharrayindexcandiffer. Example: int[][] jagArray = new int[5][]; Intheabovedeclarationtherowsarefixedinsize.Butcolumnsarenotspecifiedastheycanvary. Declaringandinitializingjaggedarray int[][] jaggedArray = new int[5][]; jaggedArray[0] = new int[3]; jaggedArray[1] = new int[5]; jaggedArray[2] = new int[2]; jaggedArray[3] = new int[8]; jaggedArray[4] = new int[10]; jaggedArray[0] = new int[] { 3, 5, 7, }; jaggedArray[1] = new int[] { 1, 0, 2, 4, 6 }; jaggedArray[2] = new int[] { 1, 6 }; jaggedArray[3] = new int[] { 1, 0, 2, 4, 6, 45, 67, 78 }; jaggedArray[4] = new int[] { 1, 0, 2, 4, 6, 34, 54, 67, 87, 78 }; Youcanalsoinitializethearrayupondeclarationlikethis: int[][] jaggedArray = new int[][] { new int[] { 3, 5, 7, }, new int[] { 1, 0, 2, 4, 6 }, new int[] { 1, 6 }, new int[] { 1, 0, 2, 4, 6, 45, 67, 78 } }; Youcanusethefollowingshorthandform: int[][] jaggedArray = { new int[] { 3, 5, 7, }, new int[] { 1, 0, 2, 4, 6 }, new int[] {1, 2, 3, 4, 5, 6, 7, 8}, new int[] {4, 5, 6, 7, 8} }; Note:Noticethatyoucannotomitthenewoperatorfromtheelementsinitializationbecausethereisnodefaultinitializationfortheelements. Jaggedarrayreallifeexample Youcanconsiderjaggedarrayasamovieticketcounterwhereticketsellingcountersarefixed(rowsarefixed)butyoudon'tknowhowmanypeoplewillbestandingineachcounterinaqueue(columnnumbersarenotfixedcanvaryondifferentrows). Ajaggedarrayisanarrayofarrays,andthereforeitselementsarereferencetypesandareinitializedtonull. Itispossibletomixjaggedandmultidimensionalarrays.Thefollowingisadeclarationandinitializationofasingle-dimensionaljaggedarraythatcontainstwo-dimensionalarrayelementsofdifferentsizes: int[][,] jaggedArray = new int[4][,] { new int[,] { {11,23}, {58,78} }, new int[,] { {50,62}, {45,65}, {85,15} }, new int[,] { {245,365}, {385,135} }, new int[,] { {1,2}, {4,4}, {4,5} } }; WeshouldbecarefulusingmethodslikeGetLowerBound(),GetUpperBound,GetLength,etc.withjaggedarrays.Sincejaggedarraysareconstructedoutofsingle-dimensionalarrays,theyshouldn'tbetreatedashavingmultipledimensionsinthesamewaythatrectangulararraysdo. Practicaldemonstrationofjaggedarrayofintegers using System; namespace jagged_array { class Program { static void Main(string[] args) { // Declare the array of four elements: int[][] jaggedArray = new int[4][]; // Initialize the elements: jaggedArray[0] = new int[2] { 7, 9 }; jaggedArray[1] = new int[4] { 12, 42, 26, 38 }; jaggedArray[2] = new int[6] { 3, 5, 7, 9, 11, 13 }; jaggedArray[3] = new int[3] { 4, 6, 8 }; // Display the array elements: for (int i = 0; i
延伸文章資訊
- 1Jagged Array in C# - C# Corner
The elements of a jagged array can be of different dimensions and sizes. A jagged array is someti...
- 2C# | Jagged Arrays - GeeksforGeeks
- 3不規則陣列- C# 程式設計手冊 - Microsoft Docs
Assign 77 to the second element ([1]) of the first array ([0]): jaggedArray3[0][1] = 77; // Assig...
- 4c# - Initializing jagged arrays - Stack Overflow
How to create jagged arrays in c#? - Stack Overflow
- 5Jagged array - Wikipedia
In computer science, a jagged array, also known as a ragged array, is an array of arrays of which...