C# Permutation of an array of arraylists?

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

    class Program
    {
        static void Main(string[] args)
        {
            var listofInts = new List>(3);
            listofInts.Add(new List{1, 2, 3});
            listofInts.Add(new List { 4,5,6 });
            listofInts.Add(new List { 7,8,9,10 });
    
            var temp = CrossJoinLists(listofInts);
            foreach (var l in temp)
            {
                foreach (var i in l)
                    Console.Write(i + ",");
                Console.WriteLine();
            }
        }
    
        private static IEnumerable> CrossJoinLists(IEnumerable> listofObjects)
        {
            var result = from obj in listofObjects.First()
                         select new List {obj};
    
            for (var i = 1; i < listofObjects.Count(); i++)
            {
                var iLocal = i;
                result = from obj  in result
                         from obj2 in listofObjects.ElementAt(iLocal)
                         select new List(obj){ obj2 };
            }
    
            return result;
        }
    }
    

提交回复
热议问题