Can not figure out how to print second dimension in three dimension List

79 views Asked by At

I am making lists in three dimensions. If I run my code it shows the following strings: list5 + list3 + list1. Now this is in three dimensions, so each dimension adds a word. If you look to the code there will be displayed: 4*4*4 = 64 possiblities. Now I want to add something to the second dimension: testing1 and testing2:

SecondDimonsion.Add("testing1"); SecondDimonsion.Add("testing2");

I dont want the result of testing to interact with List5. I do want them to interact with List1 tho. So the outcome would be:

Hello1testing1 Hello2testing1 Hello3testing1 Hello4testing1 Hello1testing2 Hello2testing2 Hello3testing2 Hello4testing2

So in total it would print 64 + 8 possibilities.

Its a hard question to explain and probably even harder to understand. I would love to edit my question if anyone doenst understand the concept.

Thanks in advance!

List<string> List1 = new List<string>();
List<string> SecondDimonsion = new List<string>();
List<string> List5 = new List<string>();
List<string> List3 = new List<string>();
List<List<List<string>>> List6 = new List<List<List<string>>>();    

List1.Add("Hello1");                  
List1.Add("Hello2");             
List1.Add("Hello3");            
List1.Add("Hello4");       

List3.Add("123");                  
List3.Add("456");             
List3.Add("789");            
List3.Add("000"); 

List5.Add("white");
List5.Add("green");             
List5.Add("yellow");            
List5.Add("black"); 

SecondDimonsion.Add("testing1");
SecondDimonsion.Add("testing2");

for (int i = 0; i < List1.Count; i++)
{
    List<List<string>> List2 = new List<List<string>>();
    for (int j = 0; j < List3.Count -2;j++)
    {
        List<string> List4 = new List<string>();
        for (int k = 0; k < List5.Count; k++)
        {
            List4.Add(List1[i] + List3[j] + List5[k]);
        }
        List2.Add(List4);
    }
    List2.Add(SecondDimonsion);
    List6.Add(List2);
}
for (int k = 0; k < List1.Count; k++)
{
    for (int i = 0; i < List3.Count -2; i++)
    {
        for (int j = 0; j < List5.Count; j++)
        {
            Console.WriteLine(List6[k][i][j]);
        }
        Console.WriteLine(List6[k][i]);
    }
}
1

There are 1 answers

1
CuddleBunny On BEST ANSWER

It looks like you're building 2 lists. The first one is 3d and the other is 2d. I would split it up like this:

    List<string> stuff1 = new List<string>();
    List<string> stuff2 = new List<string>();
    List<string> stuff3 = new List<string>();

    List<string> otherStuff = new List<string>();

    stuff1.Add("a1");
    stuff1.Add("a2");
    stuff1.Add("a3");
    stuff1.Add("a4");

    stuff2.Add("b1");
    stuff2.Add("b2");
    stuff2.Add("b3");
    stuff2.Add("b4");

    stuff3.Add("c1");
    stuff3.Add("c2");
    stuff3.Add("c3");
    stuff3.Add("c4");

    otherStuff.Add("d1");
    otherStuff.Add("d2");

    List<List<List<string>>> first64 = new List<List<List<string>>>();
    List<List<string>> other8 = new List<List<string>>();

    foreach (var v1 in stuff1) {
        // Fill temporary 2d list.
        List<List<string>> list2d = new List<List<string>>();           
        foreach (var v2 in stuff2) {
            // Fill temporary 1d list.
            List<string> list1d = new List<string>();
            foreach(var v3 in stuff3) {
                list1d.Add(v1 + v2 + v3);
            }

            // Add each 1d list to the temp 2d list.
            list2d.Add(list1d);
        }

        // Add each 2d list to the main 3d list.
        first64.Add(list2d);

        // Create another 1d list to hold second combinations.
        List<string> otherList1d = new List<string>();
        foreach(var otherV in otherStuff) {
            otherList1d.Add(v1 + otherV);
        }

        // Add second 1d list to second 2d list.
        other8.Add(otherList1d);
    }

    // Print first 64.
    for(var x = 0; x < first64.Count; x++) {
        for(var y = 0; y < first64[x].Count; y++) {
            for(var z = 0; z < first64[x][y].Count; z++) {
                Console.WriteLine(first64[x][y][z]);
            }
        }
    }

    // Print other 8.
    for(var x = 0; x < first64.Count; x++) {
        for(var y2 = 0; y2 < other8[x].Count; y2++) {
            Console.WriteLine(other8[x][y2]);
        }
    }

If you change your last loop to:

    for(var x = 0; x < List6.Count; x++) {
        for(var y = 0; y < List6[x].Count; y++) {
            for(var z = 0; z < List6[x][y].Count; z++) {
                Console.WriteLine(List6[x][y][z]);
            }
        }
    }

You will see that what you wrote actually does add SecondDimension's values to your list. Since List6[x].Count > List3.Count the loops never got there though. However you still haven't appended anything to them so you won't quite get the result you wanted.

Here is a fiddle you can play around in.