TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Jagged Arrays

C# Jagged Arrays for beginners and professionals with examples on overloading, method overriding, inheritance, aggregation, base, polymorphism, sealed, abstract, interface, namespaces, exception handling, file io, collections, multithreading, reflection etc.

<< Back to C

C# Jagged Arrays

In C#, jagged array is also known as "array of arrays" because its elements are arrays. The element size of jagged array can be different.

Declaration of Jagged array

Let's see an example to declare jagged array that has two elements.

        int[][] arr = new int[2][];

Initialization of Jagged array

Let's see an example to initialize jagged array. The size of elements can be different.

        arr[0] = new int[4];
        arr[1] = new int[6];

Initialization and filling elements in Jagged array

Let's see an example to initialize and fill elements in jagged array.

arr[0] = new int[4] { 11, 21, 56, 78 };       
arr[1] = new int[6] { 42, 61, 37, 41, 59, 63 };

Here, size of elements in jagged array is optional. So, you can write above code as given below:

arr[0] = new int[] { 11, 21, 56, 78 };       
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };

C# Jagged Array Example

Let's see a simple example of jagged array in C# which declares, initializes and traverse jagged arrays.

public class JaggedArrayTest
{
    public static void Main()
    {
        int[][] arr = new int[2][];// Declare the array

        arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array        
        arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };

        // Traverse array elements
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < arr[i].Length; j++)
            {
                System.Console.Write(arr[i][j]+" ");
            }
            System.Console.WriteLine();
        }
    }
}

Output:

11 21 56 78
42 61 37 41 59 63

Initialization of Jagged array upon Declaration

Let's see an example to initialize the jagged array while declaration.

int[][] arr = new int[3][]{
        new int[] { 11, 21, 56, 78 },
        new int[] { 2, 5, 6, 7, 98, 5 },
        new int[] { 2, 5 }
        };

C# Jagged Array Example 2

Let's see a simple example of jagged array which initializes the jagged arrays upon declaration.

public class JaggedArrayTest
{
    public static void Main()
    {
        int[][] arr = new int[3][]{
        new int[] { 11, 21, 56, 78 },
        new int[] { 2, 5, 6, 7, 98, 5 },
        new int[] { 2, 5 }
        };

        // Traverse array elements
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < arr[i].Length; j++)
            {
                System.Console.Write(arr[i][j]+" ");
            }
            System.Console.WriteLine();
        }
    }
}

Output:

11 21 56 78
2 5 6 7 98 5
2 5
Next TopicC# Params




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf