5

Can i convert List<int>to List<List<int>> in c#? When I use such construction

List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>>(element);

I get a fault,but can I do this in another way?

10
  • 5
    what do you want to achieve? Apr 10, 2013 at 9:01
  • I have data which have ID and vectors of integers .And I must save this vector in new super list?Can you understand me? Apr 10, 2013 at 9:06
  • @MykhailoVashchuk: You should create a class that holds all you required information, and then have a single list which is a collection of these items. I would post an example, but I feel it wont be answering the question as it stands
    – musefan
    Apr 10, 2013 at 9:07
  • 2
    @musefan Why should we feel ashamed? Someone asked a question, we answered. If everybody refused to answer, just because he/she feels the answer should be easy or obvious, why would users like Jon Skeet even waste their time to grace us with their knowledge?
    – Nolonar
    Apr 10, 2013 at 9:11
  • 2
    @musefan - disagree entirely. The OP was clearly struggling to use a collection initializer but worded the question incorrectly, however it was clear to me and everyone else what he was trying to achieve. With regards to the easy rep wagon, I have gained 0 rep from answering this question and the highest vote is 3, therefore I don't possibly see how you can justify saying that.
    – Darren
    Apr 10, 2013 at 9:14

6 Answers 6

12

You can do it like this:

List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);
1
  • 1
    @MykhailoVashchuk Don't forget to accept whichever answer you feel is most useful to you ;)
    – Nolonar
    Apr 10, 2013 at 9:12
8
List<int>element=new List<int>();
List<List<int>>superElement= new List<List<int>> { element };

That'll work. You can't pass the list in the constructor.

7

Try this :

var element = new List<int>();
var superElement = new List< List<int> >(){ element };
5

Yes. You can use the collection initializer

List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>> { element };

http://msdn.microsoft.com/en-gb/library/vstudio/bb384062.aspx

4

If you just want to initialize the List, then

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

works; if you want it to contain one (empty) element initially then

List<List<int>>superElement= new List<List<int>>{element};

will do it.

3

Due to the fact I do not believe the other answers have really put in much effort to help, I am going to post what I believe to be a more complete and useful answer.

To start with, and for completeness I will show how to achieve what was actually trying to be done. To create a List of Lists you can do this:

List<int> element = new List<int>();
List<List<int>> superElement = new List<List<int>>();
superElement.Add(element);

But as it stands, it doesn't make a lot of sense why you would want to do this, and after some comment probing (hope it didn't hurt) we have discovered that you want to pair up an ID with a List of integers. I would suggest taking a different approach to this.

Personally, I would create a class to hold my data, and then create a single List for those items, like so:

public class MyData
{
    public int ID {get; set;}
    public List<int> MyValues {get;set;}

    public MyData()
    {
        MyValues = new List<int>();
    }
}

Then you can do this:

List<int> element = new List<int>();
MyData data = new MyData();
data.ID = 1;
data.MyValues = element;

List<MyData> superElement = new List<MyData>();
superElement.Add(data);

Which would allow querying like so:

MyData data1 = superElement.SingleOrDeafult(x => x.ID == 1);
List<int> element = data1.MyValues;

Assuming you have Linq available.


An alternate method could be to use a dictionary, like so:

Dictionary<int, List<int>> superElement = new Dictionary<int, List<int>>();
superElement.Add(1, element);

where 1 is an ID, which you can call like so:

List<int> element = superElement[1];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.