C# Permutation of an array of arraylists?

后端 未结 13 937
难免孤独
难免孤独 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:46

    This will work no matter how many arrays you add to your myList:

            static void Main(string[] args)
            {
                string[][] myList = new string[3][];
                myList[0] = new string[] { "1", "5", "3", "9" };
                myList[1] = new string[] { "2", "3" };
                myList[2] = new string[] { "93" };
    
                List permutations = new List(myList[0]);
    
                for (int i = 1; i < myList.Length; ++i)
                {
                    permutations = RecursiveAppend(permutations, myList[i]);
                }
    
                //at this point the permutations variable contains all permutations
    
            }
    
            static List RecursiveAppend(List priorPermutations, string[] additions)
            {
                List newPermutationsResult = new List();
                foreach (string priorPermutation in priorPermutations)
                {
                    foreach (string addition in additions)
                    {
                        newPermutationsResult.Add(priorPermutation + ":" + addition);
                    }
                }
                return newPermutationsResult;
            }
    

    Note that it's not really recursive. Probably a misleading function name.

    Here is a version that adheres to your new requirements. Note the section where I output to console, this is where you can do your own formatting:

    static void Main(string[] args)
            {
                string[][] myList = new string[3][];
                myList[0] = new string[] { "1", "5", "3", "9" };
                myList[1] = new string[] { "2", "3" };
                myList[2] = new string[] { "93" };
    
                List> permutations = new List>();
    
                foreach (string init in myList[0])
                {
                    List temp = new List();
                    temp.Add(init);
                    permutations.Add(temp);
                }
    
                for (int i = 1; i < myList.Length; ++i)
                {
                    permutations = RecursiveAppend(permutations, myList[i]);
                }
    
                //at this point the permutations variable contains all permutations
    
                foreach (List list in permutations)
                {
                    foreach (string item in list)
                    {
                        Console.Write(item + ":");
                    }
                    Console.WriteLine();
                }
    
            }
    
            static List> RecursiveAppend(List> priorPermutations, string[] additions)
            {
                List> newPermutationsResult = new List>();
                foreach (List priorPermutation in priorPermutations)
                {
                    foreach (string addition in additions)
                    {
                        List priorWithAddition = new List(priorPermutation);
                        priorWithAddition.Add(addition);
                        newPermutationsResult.Add(priorWithAddition);
                    }
                }
                return newPermutationsResult;
            }
    

提交回复
热议问题