Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multidimension arraylist c#

Tags:

c#

c#-2.0

I want to create a multi-dimension ArrayList - I don't know the size, it should be decided at runtime.

how will I do that and how to access it?

It will be array of integers.

like image 577
AMH Avatar asked Jun 22 '26 03:06

AMH


1 Answers

Use

List<List<Integer>>

List by itself isn't multidimensional -- but you can use it to store Lists, which can then store Integers, in effect acting as a multidimensional array. You can then access elements as:

// Get the element at index x,y
int element = list[x][y];

To populate the list with initial elements, with dimensions x and y:

for (int i=0; i<x; i++)
{
    // Have to create the inner list for each index, or it'll be null
    list.Add(new List<Integer>());

    for (int j=0; j<y; j++)
    {
        list[i].Add(someValue); // where someValue is whatever starting value you want
    }
}
like image 194
Zach Avatar answered Jun 24 '26 17:06

Zach



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!