Nested list in C# with layers

I am not an expert coder and I have just started learning about Generic data types. What I am trying to code is a List inside of a list inside of a list .

For example- at the first level - I want numbers each inside of a list

Layer 1 list1=[1,2,3,4,5,6,7,8,9,10......1000] ,

At the 2nd layer I'd like list from 1st layer added as an element to a parent list 2 when layer 1 meets certain condition- ex layer 1 reaches max=10

List2=[List1[0:10],List1[10:20],List1[20:30], .....] ,

At layer 3, i'd like to repeat the process, all elements of layer 2 get added as an element to a new parent list 3 when layer 2 meets a max condition. list3[0]=[list2[0:10],list2[10:20],list2[20:30]..... and so on.

Only the first layer would have integer values, the successive layer would be a wrapping of it's the previous layer. How can I code this in C#?

Thanks!

1 answer

  • answered 2022-05-07 06:10 Ibrennan208

    The code below has a method called WrapInts that will create a list of ints. It then calls the method named Wrapper that takes an IEnumerable as well as returns an IEnumerable. Wrapper creates a new list, and then wraps the parameter list in the result list, before finally returning the method call to wrap the list again, infinitely. All of this can be done because a List implements the IEnumerable interface. I added a failsafe that would stop it from infinitely repeating, but you can take that out to see how close to infinity it can actually get 😉 .

    This code should accomplish what you are asking:

            public void WrapInts()
            {
                // creates a list of ints
                List<int> ints = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    
                // wraps it
                Wrapper(ints);
            }
    
            private IEnumerable Wrapper(IEnumerable listToWrap, int recursionCount = 0)
            {
                
                // if we are too deep this allows us a way out. 
                // feel free to remove it and see what happens
                if (recursionCount > 50)
                    return listToWrap;
    
                // wrap your list in a list
                List<IEnumerable> result = new List<IEnumerable>() { listToWrap };
    
                // wrap it again, infinitely
                // the ++ is in front of recursionCount to ensure we increment it prior to it being passed in
                return Wrapper(result, ++recursionCount);
            }
    

    EDIT: To address edits in the question

    I believe what you are actually trying to do is batch information, or group it in meaningful ways, to an extent.

    This answers a different question than your original, but should match what you are actually looking for:

            private void WrapInts()
            {
                // creates a list of ints
                List<object> ints = new List<object>();
    
                for(int i = 0; i < 1000; i++)
                {
                    ints.Add(i);
                }
    
                // sets our new list to the result of WrapList
                List<object> desiredList = WrapList(ints);
            }
    
            private List<object> WrapList(List<object> listToWrap, int layerMax = 10)
            {
                // the return value
                List<object> result = new List<object>();
             
                // we iterate through the list, adding objects to our result.
                int currCount = 0;
                while (listToWrap.Count > 0)
                {
                    // this temp list will be the layer max amount added
                    List<object> tempList = new List<object>();
    
                    // for each item in the current list
                    for (int i = 0; i < listToWrap.Count; i++)
                    {
                        // add it to our temp list
                        tempList.Add(listToWrap[i]);
                        // increase our current count
                        currCount++;
    
                        // if we have added up to our layer max, break out of for loop
                        if (currCount >= layerMax)
                            break;
                    }
    
                    // remove the items we processed
                    listToWrap.RemoveRange(0, currCount);
    
                    // reset currCount
                    currCount = 0;
    
                    // add the temp list to our result
                    result.Add(tempList);
                }
    
                // if the result only has one list in it, return it
                if (result.Count <= 1)
                    return result;
    
                // otherwise try "wrapping" it again
                return WrapList(result, layerMax);
            }
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum