C# Permutation of an array of arraylists?

后端 未结 13 917
难免孤独
难免孤独 2020-11-27 18:10

I have an ArrayList[] myList and I am trying to create a list of all the permutations of the values in the arrays.

EXAMPLE: (all values are strings)

         


        
13条回答
  •  生来不讨喜
    2020-11-27 18:45

    One of the problems I encountred when I was doing this for a very large amount of codes was that with the example brian was given I actually run out of memory. To solve this I used following code.

    static void foo(string s, List x, int a)
        {
            if (a == x.Count)
            {
                // output here
                Console.WriteLine(s);
            }
            else
            {
                foreach (object y in x[a])
                {
                    foo(s + y.ToString(), x, a + 1);
                }
            }
        }
    
    static void Main(string[] args)
        {
            List a = new List();
            a.Add(new string[0]);
            a.Add(new string[0]);
            a.Add(new string[0]);
            a[0] = new string[] { "T", "Z" };
            a[1] = new string[] { "N", "Z" };
            a[2] = new string[] { "3", "2", "Z" };
    
            foo("", a, 0);
            Console.Read();
        }
    

提交回复
热议问题