Advertisement
type[][] array-name;
where:
//C# Example of two-dimensional jagged array
using System;
class A
{
public static void Main()
{
//Declaration of a 2-D jagged array, which will hold multiple 1D arrays of int values.
int[][] twoDA1;
//Declaring a 2-D jagged array holding 1D arrays of object references of the type class A.
A[][] twoDA2;
}
}
type[][] array-name = new type[first-dimension][]
where:
//C# Example of constructing a two-dimensional array
using System;
class A
{
public static void Main()
{
//A 2D jagged Array is constructed on the heap with a size to hold 3 1D int arrays.
//Where, we are still to define the total number of elements in each of these 1D arrays.
int[][] twoDA1= new int[3][];
//First 1D array will have 4 int values
twoDA1[0] = new int[4];
//Second 1D array will have 2 int values
twoDA1[1] = new int[2];
//Third 1D array will have 3 int values
twoDA1[2] = new int[3];
//Prints the total number of 1D array contains in this 2D jagged array
Console.WriteLine("Total number of 1D arrays in a 2D jagged array :" + twoDA1.Length);
//Reading the elements of the first 1D array by using its index 0
Console.WriteLine("Reading the {0} elements of the first 1D array:", twoDA1[0].Length);
for(int i=0; i<4; i++)
Console.WriteLine(twoDA1[0][i]);
//Reading the elements of the second 1D array by using its index 1
Console.WriteLine("Reading the {0} elements of the second 1D array:", twoDA1[1].Length);
for(int i=0; i<2; i++)
Console.WriteLine(twoDA1[1][i]);
//Reading the elements of the third 1D array by using its index 2
Console.WriteLine("Reading the {0} elements of the third 1D array:", twoDA1[2].Length);
for(int i=0; i<3; i++)
Console.WriteLine(twoDA1[2][i]);
}
}
Total number of 1D arrays in a 2D jagged array :3
Reading the 4 elements of the first 1D array:
0
0
0
0
Reading the 2 elements of the second 1D array:
0
0
Reading the 3 elements of the third 1D array:
0
0
0
Array type | Default value |
---|---|
char | '\u0000' |
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0 |
double | 0 |
bool | false | String | null |
Object | null |
Advertisement
//C# Example of initializing a two-dimensional array
using System;
class A
{
public static void Main()
{
//A 2D jagged Array is constructed on the heap with a size to hold 3 1D int arrays.
//Where, we are still to define the total number of elements in each of these 1D arrays.
int[][] twoDA1= new int[3][];
//First 1D array will have 4 int values
twoDA1[0] = new int[4];
//Second 1D array will have 2 int values
twoDA1[1] = new int[2];
//Third 1D array will have 3 int values
twoDA1[2] = new int[3];
//Prints the total number of 1D array contains in this 2D jagged array
Console.WriteLine("Total number of 1D arrays in a 2D jagged array :" + twoDA1.Length);
//Initializing the elements of the first 1D array by using its index 0
Console.WriteLine("Initializing the {0} elements of the first 1D array ", twoDA1[0].Length);
for(int i=0; i<4; i++)
twoDA1[0][i] = i + 10;
//Initializing the elements of the second 1D array by using its index 1
Console.WriteLine("Initializing the {0} elements of the second 1D array ", twoDA1[1].Length);
for(int i=0; i<2; i++)
twoDA1[1][i] = i + 20;
//Initializing the elements of the third 1D array by using its index 2
Console.WriteLine("Initializing the {0} elements of the third 1D array", twoDA1[2].Length);
for(int i=0; i<3; i++)
twoDA1[2][i] = i + 30;
//Reading the elements of the first 1D array by using its index 0
Console.WriteLine("Reading the {0} elements of the first 1D array:", twoDA1[0].Length);
for(int i=0; i<4; i++)
Console.WriteLine(twoDA1[0][i]);
//Reading the elements of the second 1D array by using its index 1
Console.WriteLine("Reading the {0} elements of the second 1D array:", twoDA1[1].Length);
for(int i=0; i<2; i++)
Console.WriteLine(twoDA1[1][i]);
//Reading the elements of the third 1D array by using its index 2
Console.WriteLine("Reading the {0} elements of the third 1D array:", twoDA1[2].Length);
for(int i=0; i<3; i++)
Console.WriteLine(twoDA1[2][i]);
}
}
Total number of 1D arrays in a 2D jagged array :3
Initializing the 4 elements of the first 1D array
Initializing the 2 elements of the second 1D array
Initializing the 3 elements of the third 1D array
Reading the 4 elements of the first 1D array:
10
11
12
13
Reading the 2 elements of the second 1D array:
21
22
Reading the 3 elements of the third 1D array:
30
31
32
Depiction of the initializing the 2D jagged array is shown in the picture below.
Advertisement
// C# 2-D Jagged Array holding arrays of object references
using System;
class A
{
public static void Main()
{
//A 2D jagged Array is constructed on the heap with a size to hold 3 1D arrays to hold object references of A.
//But, we are still to define the total number of object references/elements in each of these 1D arrays.
A[][] twoDA1= new A[3][];
//First 1D array will have 4 object references of A
twoDA1[0] = new A[4];
//Second 1D array will have 2 object references of A
twoDA1[1] = new A[2];
//Third 1D array will have 3 object references of A
twoDA1[2] = new A[3];
//Prints the total number of 1D array contains in this 2D jagged array
Console.WriteLine("Total number of 1D arrays in a 2D jagged array :" + twoDA1.Length);
//Initializing the elements of the first 1D array by using its index 0
Console.WriteLine("Initializing the {0} elements of the first 1D array ", twoDA1[0].Length);
for(int i=0; i<4; i++)
twoDA1[0][i] = new A();
//Initializing the elements of the second 1D array by using its index 1
Console.WriteLine("Initializing the {0} elements of the second 1D array ", twoDA1[1].Length);
for(int i=0; i<2; i++)
twoDA1[1][i] = new A();
//Initializing the elements of the third 1D array by using its index 2
Console.WriteLine("Initializing the {0} elements of the third 1D array", twoDA1[2].Length);
for(int i=0; i<3; i++)
twoDA1[2][i] = new A();
//Prints the total number of 1D array contains in this 2D jagged array
Console.WriteLine("Total number of 1D arrays in a 2D jagged array :" + twoDA1.Length);
//Reading the elements of the second 1D array by using its index 0
Console.WriteLine("Reading the {0} elements of the first 1D array:", twoDA1[0].Length);
for(int i=0; i<4; i++)
Console.WriteLine(twoDA1[0][i]);
//Reading the elements of the second 1D array by using its index 1
Console.WriteLine("Reading the {0} elements of the second 1D array:", twoDA1[1].Length);
for(int i=0; i<2; i++)
Console.WriteLine(twoDA1[1][i]);
//Reading the elements of the third 1D array by using its index 2
Console.WriteLine("Reading the {0} elements of the third 1D array:", twoDA1[2].Length);
for(int i=0; i<3; i++)
Console.WriteLine(twoDA1[2][i]);
}
}
Total number of 1D arrays in a 2D jagged array :3
Initializing the 4 elements of the first 1D array
Initializing the 2 elements of the second 1D array
Initializing the 3 elements of the third 1D array
Total number of 1D arrays in a 2D jagged array :3
Reading the 4 elements of the first 1D array:
A
A
A
A
Reading the 2 elements of the second 1D array:
A
A
Reading the 3 elements of the third 1D array:
A
A
A
//C# A 2D jagged array holding object references will be automatically initialized to null
using System;
class A
{
public static void Main()
{
//A 2D jagged Array is constructed on the heap with a size to hold 3 1D arrays to hold object references of A.
//But, we are still to define the total number of object references/elements in each of these 1D arrays.
A[][] twoDA1= new A[3][];
//First 1D array will have 4 object references of A
twoDA1[0] = new A[4];
//Second 1D array will have 2 object references of A
twoDA1[1] = new A[2];
//Third 1D array will have 3 object references of A
twoDA1[2] = new A[3];
//Prints the total number of 1D array contains in this 2D jagged array
Console.WriteLine("Total number of 1D arrays in a 2D jagged array :" + twoDA1.Length);
//Reading the elements of the second 1D array by using its index 0
Console.WriteLine("Reading the {0} elements of the uninitialized first 1D array:", twoDA1[0].Length);
for(int i=0; i<4; i++)
{
if(twoDA1[0][i]==null)
Console.WriteLine(twoDA1[0][i] + "A null element");
}
//Reading the elements of the second 1D array by using its index 1
Console.WriteLine("Reading the {0} elements of the uninitialized second 1D array:", twoDA1[1].Length);
for(int i=0; i<2; i++)
{
if(twoDA1[1][i]==null)
Console.WriteLine(twoDA1[1][i] + "A null element");
}
//Reading the elements of the third 1D array by using its index 2
Console.WriteLine("Reading the {0} elements of the uninitialized third 1D array:", twoDA1[2].Length);
for(int i=0; i<3; i++)
{
if(twoDA1[2][i]==null)
Console.WriteLine(twoDA1[2][i] + "A null element");
}
}
}
Total number of 1D arrays in a 2D jagged array :3
Reading the 4 elements of the uninitialized first 1D array:
A null element
A null element
A null element
A null element
Reading the 2 elements of the uninitialized second 1D array:
A null element
A null element
Reading the 3 elements of the uninitialized third 1D array:
A null element
A null element
A null element
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement