Exforsys
+ Reply to Thread
Results 1 to 2 of 2

diff bet multi dimension array and jagged array

This is a discussion on diff bet multi dimension array and jagged array within the Microsoft .NET forums, part of the Programming Talk category; Hi, Can any one tell me what is the difference between Multidimension array and Jagged array? - Sri Hari...

  1. #1
    Sri82 is offline Junior Member Array
    Join Date
    Feb 2008
    Answers
    1

    diff bet multi dimension array and jagged array

    Hi,

    Can any one tell me what is the difference between Multidimension array and Jagged array?

    - Sri Hari



  2. #2
    techvinny is offline Moderator Array
    Join Date
    Dec 2010
    Answers
    56
    A multi-dimensional array has fixed columns for each of the rows...

    Code:
    //Creates a 2-dimensional array with 4 rows and every row with 2 columns, 4x2
    int[,] array = new int[4, 2];
    
    //Creates a 3-dimensional array of 4x2x3
    int[, ,] array1 = new int[4, 2, 3];
    
    //initialzing 2-dimensional array on declaration using new
    int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
    
    //initializing 3-dimensional array on declaration without using new or specifying rank
    int[, ,] array3D = { { { 1, 2, 3 } }, { { 4, 5, 6 } } };
    
    //assigning value to an array element
    array1[1, 0, 2] = 27;
    A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays. Its elements are reference types and are initialized to null.

    Code:
    //declaration of a single-dimensional array that has three elements, each of which is a single-dimensional array of integers
    int[][] jaggedArray = new int[3][];
    
    //initializing the elements of a jagged array. it's a must to initialize each of the elements before using them
    jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };  //array of 5 integers
    jaggedArray[1] = new int[] { 0, 2, 4, 6 };     //array of 4 integers
    jaggedArray[2] = new int[] { 11, 22 };         //array of 2 integers
    
    //You can also initialize the array elements without using new
    int[][] jaggedArray3 = 
    {
        new int[] {1,3,5,7,9},
        new int[] {0,2,4,6},
        new int[] {11,22}
    };
    
    // Assign value to the second element ([1]) of the first array ([0]
    jaggedArray3[0][1] = 34;
    HTH!!!


Latest Article

Network Security Risk Assessment and Measurement

Read More...