locked
How to declare a dynamic 2D Array? RRS feed

  • Question

  • This is a baisc question, I hope. How do I declare a dynamic 2D array in C#?
    Thursday, May 31, 2007 4:33 PM

Answers

  •  

    Hi

     

    Herre is the example for creating the runtime creating array.

     

    Code Snippet

    int strnoofRow = int.Parse(Console.ReadLine());

    int strnoofCol = int.Parse(Console.ReadLine());

    String [,]arr1=new String[strnoofRow,strnoofCol];

    arr1[0,0] = "Nayan";

    Console.WriteLine(arr1[0,0]);

     

    Array arr = Array.CreateInstance(typeof(String), strnoofRow, strnoofCol);

    arr.SetValue("Nayan",0,0);

    Console.WriteLine(arr.GetValue(0,0));

     

     

     

    Thursday, May 31, 2007 6:20 PM
  • Do you need no specified length to the array in only 1 dimension or both?

    I think you'd do better with a Jagged Array than a traditional multidimensional array. There are some differences that you'll have to pay attention to, but Jagged arrays work better as dynamic arrays.
    Jagged arrays are the declaration
    int[][] instead of int[,]
    Where you can actually have
    ArrayList a = new ArrayList(); // One layer of the Jagged array, dynamic sizing
    int[] i = new int[10];
    a.Add(i); // Start of the jagged array, you have a [1][10] array

    Note: If you still need accessing via [,] I would suggest implenting your own 2d class that stores internally as a jagged array and has declared indexers.

    ArrayList stores everything as Object, thus whatever is inside must be derived from Object. Since this is everything in C#, you can store an array as an element of ArrayList. Though it may be better to do a generic List for typing.

    Read up on
    Arrays C#
    JaggedArrays
    ArrayList
    List<>


    If you provide a bit more information about the type of data and method it needs to be stored in your dynamic 2d array, we can tailor examples to better fit your needs.
    Thursday, May 31, 2007 9:00 PM
  • Hi, Charlie

    A simple int 2D int array definition and initialization should be like:

    Code Snippet

    int[,] i1;

    i1 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

     

    And further more you may want jagged array as follows

    Code Snippet
    int[][] i = new int[2][];
                i[0] = new int[3] { 1, 2, 3 };
                i[1] = new int[2] { 4, 5 };

     

    Thanks

    Monday, June 4, 2007 3:42 AM

All replies

  •  

    Hi

     

    Herre is the example for creating the runtime creating array.

     

    Code Snippet

    int strnoofRow = int.Parse(Console.ReadLine());

    int strnoofCol = int.Parse(Console.ReadLine());

    String [,]arr1=new String[strnoofRow,strnoofCol];

    arr1[0,0] = "Nayan";

    Console.WriteLine(arr1[0,0]);

     

    Array arr = Array.CreateInstance(typeof(String), strnoofRow, strnoofCol);

    arr.SetValue("Nayan",0,0);

    Console.WriteLine(arr.GetValue(0,0));

     

     

     

    Thursday, May 31, 2007 6:20 PM
  • dynamic as in dynamic sizing (growing array) or just dynamic definition? They are vastly different in concept.

    You can declare any array at runtime using variables for the size (row, col) but you will have to use some other collection for dynamic sizing, basic arrays can't resize themselves.

    You can do an ArrayList of ArrayLists (jagged array, but still 2D). If you're in 2.0 you can use the List<> generic of an array to get a jagged array of 2D. You can always create your own collection that can manage the 2d indexers [,] and be self-growing.
    • Proposed as answer by Amare1982 Sunday, May 10, 2009 11:05 AM
    Thursday, May 31, 2007 6:44 PM
  • Thank you so much for the help. I'll give that a try.

     

    Dynamic as there is no specified lenth of the array. I'm not sure how big or small the array will be so I need the array to grow or shrink as needed. I was working with ArrayList but ArrayList do not give me the option of 2D.

    Thursday, May 31, 2007 7:02 PM
  • Do you need no specified length to the array in only 1 dimension or both?

    I think you'd do better with a Jagged Array than a traditional multidimensional array. There are some differences that you'll have to pay attention to, but Jagged arrays work better as dynamic arrays.
    Jagged arrays are the declaration
    int[][] instead of int[,]
    Where you can actually have
    ArrayList a = new ArrayList(); // One layer of the Jagged array, dynamic sizing
    int[] i = new int[10];
    a.Add(i); // Start of the jagged array, you have a [1][10] array

    Note: If you still need accessing via [,] I would suggest implenting your own 2d class that stores internally as a jagged array and has declared indexers.

    ArrayList stores everything as Object, thus whatever is inside must be derived from Object. Since this is everything in C#, you can store an array as an element of ArrayList. Though it may be better to do a generic List for typing.

    Read up on
    Arrays C#
    JaggedArrays
    ArrayList
    List<>


    If you provide a bit more information about the type of data and method it needs to be stored in your dynamic 2d array, we can tailor examples to better fit your needs.
    Thursday, May 31, 2007 9:00 PM
  • Hi

     

    if u want to use 2d Like facility than u can use the Hashtable or Dictionary<int,int>. but it will not give u facility for difrrent diamensions

    • Proposed as answer by Amare1982 Sunday, May 10, 2009 11:06 AM
    Friday, June 1, 2007 8:06 AM
  • Hi, Charlie

    A simple int 2D int array definition and initialization should be like:

    Code Snippet

    int[,] i1;

    i1 = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } };

     

    And further more you may want jagged array as follows

    Code Snippet
    int[][] i = new int[2][];
                i[0] = new int[3] { 1, 2, 3 };
                i[1] = new int[2] { 4, 5 };

     

    Thanks

    Monday, June 4, 2007 3:42 AM
  • Hi,

    Yes, your method is better. It is way easier to work with list or arraylists as compare to traditional 2D arrays of c++.

    Amare
    • Proposed as answer by Amare1982 Monday, April 23, 2012 4:00 AM
    Wednesday, May 6, 2009 3:23 AM