70

I want to have an array of Lists. In c++ I do like:

List<int> a[100];

which is an array of 100 Lists. each list can contain many elements. I don't know how to do this in c#. Can anyone help me?

7 Answers 7

128

You do like this:

List<int>[] a = new List<int>[100];

Now you have an array of type List<int> containing 100 null references. You have to create lists and put in the array, for example:

a[0] = new List<int>();
Sign up to request clarification or add additional context in comments.

Comments

27

Since no context was given to this question and you are a relatively new user, I want to make sure that you are aware that you can have a list of lists. It's not the same as array of list and you asked specifically for that, but nevertheless:

List<List<int>> myList = new List<List<int>>();

you can initialize them through collection initializers like so:

List<List<int>> myList = new List<List<int>>(){{1,2,3},{4,5,6},{7,8,9}};

1 Comment

+1 for initializer syntax (which works for arrays as well as lists)
14

simple approach:

        List<int>[] a = new List<int>[100];
        for (int i = 0; i < a.Length; i++)
        {
            a[i] = new List<int>();
        }

or LINQ approach

        var b = Enumerable.Range(0,100).Select((i)=>new List<int>()).ToArray();

Comments

4

I can suggest that you both create and initialize your array at the same line using linq:

List<int>[] a = new List<int>[100].Select(item=>new List<int>()).ToArray();

If you want a more efficient code, you can also use:

List<int>[] a = Enumerable.Range(0, 100).Select(_ => new List<int>()).ToArray();

The reason is that it first creates an array of 100 null references to List, and then it uses LINQ to iterate over each element and replace the null reference with a new List instance. This means that the code is doing more work than necessary, as it is first creating an array of null references, and then iterating over it to create new List instances.

In contrast, the second code creates the List instances directly using Enumerable.Range and Select, without first creating an array of null references. This means that it is more efficient, as it avoids unnecessary work.

Comments

3
List<int>[]  a = new List<int>[100];

You still would have to allocate each individual list in the array before you can use it though:

for (int i = 0; i < a.Length; i++)
    a[i] = new List<int>();

Comments

1

use

List<int>[] a = new List<int>[100];

Comments

0
List<List<int>> Registers = new List<List<int>>() { };

for (int j = 0; j < 5; j++)
{
    List<int> data_1 = new List<int>();
    List<int> data_2 = new List<int>();

    data_1.Add(j * 5);
    Registers.Add(data_1);

    data_2.Add(10 + j + 1);
    data_2.Add(10 + j + 2);
    Registers.Add(data_2);

    //if you change or clear data_1 or data_2 then Registers will be change
    //data_1.Clear();
    //data_2.Clear();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.