none
List Containing List RRS feed

  • Question

  • Hello, How do i add and retrieve elements to a

     List<List<int>> myList = new List<List<int>>();
    please Help.

    Friday, June 21, 2019 1:40 AM

Answers

  • Hello,

    It all depends on what you want to do with the items.

    var myList = new List<List<int>>
    {
        new List<int>() {1, 2, 3, 4},
        new List<int>() {11, 33, 44}
    };
    
    foreach (var list in myList)
    {
        Console.WriteLine(string.Join(",",list.ToArray()));
    }

    Or

    for (int upperIndex = 0; upperIndex < myList.Count; upperIndex++)
    {
        Console.WriteLine($"{upperIndex}");
        for (int innerIndex = 0; innerIndex < myList[upperIndex].Count; innerIndex++)
        {
            Console.WriteLine($"   {myList[upperIndex][innerIndex]}");
        }
    }

    And here is an add

    myList[1].Add(100);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Friday, June 21, 2019 2:43 AM
    Moderator
  • Hi slalith,

    Thank you for posting here.

    For your question, you want to add and retrieve elements to a list of list.

    You could try the following code.

    List<List<int>> list = new List<List<int>>();
    List<int> newlist = new List<int>();//add elements
    newlist.Add(1);
    newlist.Add(15);
    newlist.Add(16);
    list.Add(newlist);
    
    
    foreach (int item in list.SelectMany(x=>x)) //retrieve elements
     {
    if(item==15)
    {
      Console.WriteLine("I find it {0}",item);
    }
    }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by slalithp Saturday, June 22, 2019 12:02 AM
    Friday, June 21, 2019 2:55 AM
    Moderator

All replies

  • Hello,

    It all depends on what you want to do with the items.

    var myList = new List<List<int>>
    {
        new List<int>() {1, 2, 3, 4},
        new List<int>() {11, 33, 44}
    };
    
    foreach (var list in myList)
    {
        Console.WriteLine(string.Join(",",list.ToArray()));
    }

    Or

    for (int upperIndex = 0; upperIndex < myList.Count; upperIndex++)
    {
        Console.WriteLine($"{upperIndex}");
        for (int innerIndex = 0; innerIndex < myList[upperIndex].Count; innerIndex++)
        {
            Console.WriteLine($"   {myList[upperIndex][innerIndex]}");
        }
    }

    And here is an add

    myList[1].Add(100);


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Friday, June 21, 2019 2:43 AM
    Moderator
  • Hello, How do i add and retrieve elements to a

     List<List<int>> myList = new List<List<int>>();
    please Help.

    What's the purpose? I have seen a list of custom objects and each object has a list of custom objects in it and each custom oject in that that list  can have a list of custom objects so on  and so on.

    [Moderator edit: Aw, come on, it wasn't so bad as to flag it as "abusive", you see worse things all of the time in the forums. Anyway, I've deleted the sentence in question to avoid trouble]





    Friday, June 21, 2019 2:45 AM
  • Hi slalith,

    Thank you for posting here.

    For your question, you want to add and retrieve elements to a list of list.

    You could try the following code.

    List<List<int>> list = new List<List<int>>();
    List<int> newlist = new List<int>();//add elements
    newlist.Add(1);
    newlist.Add(15);
    newlist.Add(16);
    list.Add(newlist);
    
    
    foreach (int item in list.SelectMany(x=>x)) //retrieve elements
     {
    if(item==15)
    {
      Console.WriteLine("I find it {0}",item);
    }
    }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by slalithp Saturday, June 22, 2019 12:02 AM
    Friday, June 21, 2019 2:55 AM
    Moderator

  • To me, what you have makes no sense. What's the purpose? 




    It's a program from HackerRank. 
    Saturday, June 22, 2019 12:00 AM
  • Thank you :) 
    Saturday, June 22, 2019 12:02 AM

  • To me, what you have makes no sense. What's the purpose? 




    It's a program from HackerRank. 

    Oh.. I had to look that up.

    https://news.efinancialcareers.com/uk-en/287595/hackerrank-tests-banking

    <copied>

    “Suddenly, banks, hedge funds and proprietary trading companies have started screening candidates using these tests provided by HackerRank and other coding sites,” says Adler. “It’s fine if you’re a masters student who’s just graduated in computer science, but if you’ve got 20-25 years’ experience working in the sector and haven’t spent your time building complex algorithms, they’re a lot less relevant.”

    <end>

    I am in the 30 years and more range with programming. :)

    Saturday, June 22, 2019 2:37 AM

  • Oh.. I had to look that up.

    https://news.efinancialcareers.com/uk-en/287595/hackerrank-tests-banking

    <copied>

    “Suddenly, banks, hedge funds and proprietary trading companies have started screening candidates using these tests provided by HackerRank and other coding sites,” says Adler. “It’s fine if you’re a masters student who’s just graduated in computer science, but if you’ve got 20-25 years’ experience working in the sector and haven’t spent your time building complex algorithms, they’re a lot less relevant.”

    <end>

    I am in the 30 years and more range with programming. :)

    Oh, okay. I will just ignore it then,I find it very complicated too. Thank you by the way.
    • Edited by slalithp Sunday, June 23, 2019 7:00 AM added thanks
    Sunday, June 23, 2019 6:59 AM

  • Oh.. I had to look that up.

    https://news.efinancialcareers.com/uk-en/287595/hackerrank-tests-banking

    <copied>

    “Suddenly, banks, hedge funds and proprietary trading companies have started screening candidates using these tests provided by HackerRank and other coding sites,” says Adler. “It’s fine if you’re a masters student who’s just graduated in computer science, but if you’ve got 20-25 years’ experience working in the sector and haven’t spent your time building complex algorithms, they’re a lot less relevant.”

    <end>

    I am in the 30 years and more range with programming. :)

    Oh, okay. I will just ignore it then,I find it very complicated too. Thank you by the way.
    Well at least you understood my questioning. Those stupid tests are just that,  stupid.  I refused to do one and just walked out from the interview process for a contract onetime. I have also refused to take  online tests too from home that some two bit consulting firms were trying to give  several years ago when that testing junk was popular. :)
    Sunday, June 23, 2019 8:39 AM

  • Well at least you understood my questioning. Those stupid tests are just that,  stupid.  I refused to do one and just walked out from the interview process for a contract onetime. I have also refused to take  online tests too from home that some two bit consulting firms were trying to give  several years ago when that testing junk was popular. :)
    Yup, Thank you so much for sharing your experience. Now i know i can ignore questions that make no sense while there is always an easy approach to it. :) 
    Sunday, June 23, 2019 12:05 PM

  • Well at least you understood my questioning. Those stupid tests are just that,  stupid.  I refused to do one and just walked out from the interview process for a contract onetime. I have also refused to take  online tests too from home that some two bit consulting firms were trying to give  several years ago when that testing junk was popular. :)

    Yup, Thank you so much for sharing your experience. Now i know i can ignore questions that make no sense while there is always an easy approach to it. :) 

    I'll share another story with you.  I and another experienced programmer that had vast knowledge in .NET programming he took over a Windows service project that a programmer with a  master's degree in computer science had written who was wet behind the ears and still had diaper rash.

    The coding was so unnecessarily complicated that it was unbelievable. It made no senses that he did it not knowing that someone else might have to maintain it. I and the other developer discussed about was the guy playing when he wrote this program  in the fact that he could do anything he wanted, pull out all the bells and whistle that he knew about and that only he could maintain it.  I told the other developer it was better him than me that had to maintain it. :)

    A lot of times you will find in programming that simpler is better, and you should think about the developer that may have to maintain something you wrote. 

    It's just a little FYI. :)


    • Edited by DA924x Sunday, June 23, 2019 2:11 PM
    Sunday, June 23, 2019 2:08 PM

  • Well at least you understood my questioning. Those stupid tests are just that,  stupid.  I refused to do one and just walked out from the interview process for a contract onetime. I have also refused to take  online tests too from home that some two bit consulting firms were trying to give  several years ago when that testing junk was popular. :)

    Yup, Thank you so much for sharing your experience. Now i know i can ignore questions that make no sense while there is always an easy approach to it. :) 

    I'll share another story with you.  I and another experienced programmer that had vast knowledge in .NET programming he took over a Windows service project that a programmer with a  master's degree in computer science had written who was wet behind the ears and still had diaper rash.

    The coding was so unnecessarily complicated that it was unbelievable. It made no senses that he did it not knowing that someone else might have to maintain it. I and the other developer discussed about was the guy playing when he wrote this program  in the fact that he could do anything he wanted, pull out all the bells and whistle that he knew about and that only he could maintain it.  I told the other developer it was better him than me that had to maintain it. :)

    A lot of times you will find in programming that simpler is better, and you should think about the developer that may have to maintain something you wrote. 

    It's just a little FYI. :)


    thank you, i have recently joined job as game programmer. I will keep this in mind that when ever i have to code some one might have to maintain it and do not make it complicated. :)
    • Edited by slalithp Monday, June 24, 2019 1:23 AM mak updated to make
    Monday, June 24, 2019 1:22 AM