C# Jagged Array (With Examples) - Programiz
文章推薦指數: 80 %
In C#, a jagged array consists of multiple arrays as its element. However, unlike multidimensional arrays, each array inside a jagged array can be of different ...
CourseIndex
ExploreProgramiz
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
C#"HelloWorld"Program
OperatorsinC#
C#ifStatement
C#foreachLoop
C#forLoop
StartLearningC#
LearningPaths
Challenges
LearnPythonInteractively
TryforFree
Courses
BecomeaPythonMaster
BecomeaCMaster
BecomeaJavaMaster
ViewallCourses
Python
JavaScript
SQL
C
C++
Java
Kotlin
Swift
C#
DSA
PopularTutorials
C#"HelloWorld"Program
OperatorsinC#
C#ifStatement
C#foreachLoop
C#forLoop
StartLearningC#
AllC#Tutorials
Python
JavaScript
C
C++
Java
Kotlin
PopularExamples
Addtwonumbers
Checkprimenumber
Findthefactorialofanumber
PrinttheFibonaccisequence
Checkleapyear
AllPythonExamples
Introduction
C#HelloWorld
C#Keywords&Identifiers
C#Variables
C#Operators
C#OperatorPrecedence
C#BitwiseOperators
C#BasicI/O
C#Expressions&Statements
C#Comments
FlowControl
C#if...else
C#switchStatement
C#TernaryOperator
C#forLoop
C#whileLoop
C#NestedLoops
C#breakStatement
C#continueStatement
Arrays
C#Arrays
C#MultidimensionalArrays
C#JaggedArray
C#foreachLoop
OOP(I)
C#ClassandObjects
C#Methods
C#AccessModifiers
C#VariableScope
C#Constructors
C#thisKeyword
C#staticKeyword
C#Strings
OOP(II)
C#Inheritance
C#AbstractClass&Methods
C#NestedClass
C#PartialClass
C#SealedClass
C#Interface
C#MethodOverloading
C#ConstructorOverloading
AdditionalTopics
C#using
C#TypeConversion
C#PreprocessorDirectives
C#Namespaces
C#struct
RelatedTopics
C#MultidimensionalArray
C#Arrays
C#foreachloop
C#StringJoin()
C#thisKeyword
C#StringSplit()
C#JaggedArray
Inthistutorial,wewilllearnabouttheC#jaggedarray.Wewilllearntodeclare,initialize,andaccessthejaggedarraywiththehelpofexamples.
InC#,ajaggedarrayconsistsofmultiplearraysasitselement.However,unlikemultidimensionalarrays,eacharrayinsideajaggedarraycanbeofdifferentsizes.
Beforeyoulearnaboutjaggedarray,makesuretoknowabout
C#Arrays
C#MultidimensionalArrays
C#JaggedArrayDeclaration
Here'sasyntaxtodeclareajaggedarrayinC#.
dataType[][]nameOfArray=newdataType[rows][];
Let'sseeanexample,
//declarejaggedarray
int[][]jaggedArray=newint[2][];
Here,
int-datatypeofthearray
[][]-representsjaggedarray
jaggedArray-nameofthejaggedarray
[2][]-representsthenumberofelements(arrays)insidethejaggedarray
Sinceweknoweachelementofajaggedarrayisalsoanarray,wecansetthesizeoftheindividualarray.Forexample,
//setsizeofthefirstarrayas3
jaggedArray[0]=newint[3];
//setsizeofsecondarrayas2
jaggedArray[1]=newint[2];
InitializingJaggedArray
Therearedifferentwaystoinitializeajaggedarray.Forexample,
1.Usingtheindexnumber
Oncewedeclareajaggedarray,wecanusetheindexnumbertoinitializeit.Forexample,
//initializethefirstarray
jaggedArray[0][0]=1;
jaggedArray[0][1]=3;
jaggedArray[0][2]=5;
//initializethesecondarray
jaggedArray[1][0]=2;
jaggedArray[1][1]=4;
Here,
indexatthefirstsquarebracketrepresentstheindexofthejaggedarrayelement
indexatthesecondsquarebracketrepresentstheindexoftheelementinsideeachelementofthejaggedarray
2.Initializewithoutsettingsizeofarrayelements
//declaringstringjaggedarray
int[][]jaggedArray=newint[2][];
//initializeeacharray
jaggedArray[0]=newint[]{1,3,5};
jaggedArray[1]=newint[]{2,4};
3.InitializewhiledeclaringJaggedArray
int[][]jaggedArray={
newint[]{10,20,30},
newint[]{11,22},
newint[]{88,99}
};
Accessingelementsofajaggedarray
Wecanaccesstheelementsofthejaggedarrayusingtheindexnumber.Forexample,
//accessfirstelementofsecondarray
jaggedArray[1][0];
//accesssecondelementofthesecondarray
jaggedArray[1][1];
//accesssecondelementofthefirstarray
jaggedArray[0][1];
Example:C#JaggedArray
usingSystem;
namespaceJaggedArray{
classProgram{
staticvoidMain(string[]args){
//createajaggedarray
int[][]jaggedArray={
newint[]{1,3,5},
newint[]{2,4},
};
//printelementsofjaggedarray
Console.WriteLine("jaggedArray[1][0]:"+jaggedArray[1][0]);
Console.WriteLine("jaggedArray[1][1]:"+jaggedArray[1][1]);
Console.WriteLine("jaggedArray[0][2]:"+jaggedArray[0][2]);
Console.ReadLine();
}
}
}
Output
jaggedArray[1][0]:2
jaggedArray[1][1]:4
jaggedArray[0][2]:5
Here,insideajaggedarray,
jaggedArray[1][0]-firstelementofthesecondarray
jaggedArray[1][1]-secondelementofthesecondarray
jaggedArray[0][2]-thirdelementofthefirstarray
Iteratingthroughajaggedarray
InC#,wecanuseloopstoiteratethrougheachelementofthejaggedarray.Forexample,
usingSystem;
namespaceJaggedArray{
classProgram{
staticvoidMain(string[]args){
//declareajaggedarray
int[][]jaggedArray=newint[2][];
//setsizeofJaggedArrayElements
jaggedArray[0]=newint[3];
jaggedArray[1]=newint[2];
//initializethefirstarray
jaggedArray[0][0]=1;
jaggedArray[0][1]=3;
jaggedArray[0][2]=5;
//initializethesecondarray
jaggedArray[1][0]=2;
jaggedArray[1][1]=2;
//outerforloop
for(inti=0;i
延伸文章資訊
- 1C# | Jagged Arrays - GeeksforGeeks
- 2Jagged Array in Java - GeeksforGeeks
A jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we ...
- 3Jagged array - Wikipedia
In computer science, a jagged array, also known as a ragged array, is an array of arrays of which...
- 4C# Jagged Arrays: An Array of Array - TutorialsTeacher
A jagged array is an array of array. Jagged arrays store arrays instead of literal values. A jagg...
- 5不規則陣列- C# 程式設計手冊 - Microsoft Docs
Assign 77 to the second element ([1]) of the first array ([0]): jaggedArray3[0][1] = 77; // Assig...