C#排列一系列的arraylists?

时间:2009-04-02 17:07:30

标签: c# arrays arraylist permutation

我有一个ArrayList [] myList,我正在尝试创建数组中值的所有排列列表。

示例:(所有值均为字符串)

myList[0] = { "1", "5", "3", "9" };
myList[1] = { "2", "3" };
myList[2] = { "93" };

myList的计数可以改变,因此事先不知道它的长度。

我希望能够生成所有排列的列表,类似于以下内容(但有一些额外的格式)。

1 2 93
1 3 93
5 2 93
5 3 93
3 2 93
3 3 93
9 2 93
9 3 93

这是否理解我想要完成的事情?我似乎无法想出这样做的好方法,(如果有的话)。

编辑:
我不确定递归是否会影响我以自己的方式格式化输出的愿望。对不起,我之前没有提到我的格式。

我想最终构建一个string []数组,其中包含以下格式的所有组合:

表示“1 2 93”排列

我希望输出为“val0 = 1; val1 = 2; val2 = 93;”

我现在将尝试递归。谢谢DrJokepu

13 个答案:

答案 0 :(得分:17)

我很惊讶没有人发布LINQ解决方案。

from val0 in new []{ "1", "5", "3", "9" }
from val1 in new []{ "2", "3" }
from val2 in new []{ "93" }
select String.Format("val0={0};val1={1};val2={2}", val0, val1, val2)

答案 1 :(得分:14)

递归解决方案

    static List<string> foo(int a, List<Array> x)
    {
        List<string> retval= new List<string>();
        if (a == x.Count)
        {
            retval.Add("");
            return retval;
        }
        foreach (Object y in x[a])
        {
            foreach (string x2 in foo(a + 1, x))
            {
                retval.Add(y.ToString() + " " + x2.ToString());
            }

        }
        return retval;
    }
    static void Main(string[] args)
    {
        List<Array> myList = new List<Array>();
        myList.Add(new string[0]);
        myList.Add(new string[0]);
        myList.Add(new string[0]);
        myList[0] = new string[]{ "1", "5", "3", "9" };
        myList[1] = new string[] { "2", "3" };
        myList[2] = new string[] { "93" };
        foreach (string x in foo(0, myList))
        {
            Console.WriteLine(x);
        }

        Console.ReadKey();
    }

请注意,通过将返回值更改为字符串列表列表并更改retval.add调用以使用列表而不是使用连接,返回列表或数组而不是字符串将非常容易。< / p>

工作原理:

这是一种经典的递归算法。基本情况是foo(myList.Count, myList),它返回一个包含一个元素的List,即空字符串。 n个字符串数组s1,s2,...,sN的列表的排列等于sA1的每个成员,其前缀为n-1个字符串数组的排列,s2,...,sN。基本情况就是为sN的每个元素提供一些内容来连接。

答案 2 :(得分:5)

我最近在我的一个项目中遇到了类似的问题,偶然发现了这个问题。我需要一个可以处理任意对象列表的非递归解决方案。这就是我想出来的。基本上我正在为每个子列表形成一个枚举器列表,并迭代地递增它们。

public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<IEnumerable<T>> lists)
{
    // Check against an empty list.
    if (!lists.Any())
    {
        yield break;
    }

    // Create a list of iterators into each of the sub-lists.
    List<IEnumerator<T>> iterators = new List<IEnumerator<T>>();
    foreach (var list in lists)
    {
        var it = list.GetEnumerator();
        // Ensure empty sub-lists are excluded.
        if (!it.MoveNext())
        {
            continue;
        }
        iterators.Add(it);
    }

    bool done = false;
    while (!done)
    {
        // Return the current state of all the iterator, this permutation.
        yield return from it in iterators select it.Current;

        // Move to the next permutation.
        bool recurse = false;
        var mainIt = iterators.GetEnumerator();
        mainIt.MoveNext(); // Move to the first, succeeds; the main list is not empty.
        do
        {
            recurse = false;
            var subIt = mainIt.Current;
            if (!subIt.MoveNext())
            {
                subIt.Reset(); // Note the sub-list must be a reset-able IEnumerable!
                subIt.MoveNext(); // Move to the first, succeeds; each sub-list is not empty.

                if (!mainIt.MoveNext())
                {
                    done = true;
                }
                else
                {
                    recurse = true;
                }
            }
        }
        while (recurse);
    }
}

答案 3 :(得分:3)

您可以使用factoradics生成排列的枚举。尝试使用this article on MSDN来实现C#中的实现。

答案 4 :(得分:2)

无论你添加到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<string> permutations = new List<string>(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<string> RecursiveAppend(List<string> priorPermutations, string[] additions)
        {
            List<string> newPermutationsResult = new List<string>();
            foreach (string priorPermutation in priorPermutations)
            {
                foreach (string addition in additions)
                {
                    newPermutationsResult.Add(priorPermutation + ":" + addition);
                }
            }
            return newPermutationsResult;
        }

请注意,它并不是真正的递归。可能是一个误导性的函数名称。

这是一个符合您新要求的版本。请注意我输出到控制台的部分,您可以在此处进行自己的格式化:

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<List<string>> permutations = new List<List<string>>();

            foreach (string init in myList[0])
            {
                List<string> temp = new List<string>();
                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<string> list in permutations)
            {
                foreach (string item in list)
                {
                    Console.Write(item + ":");
                }
                Console.WriteLine();
            }

        }

        static List<List<string>> RecursiveAppend(List<List<string>> priorPermutations, string[] additions)
        {
            List<List<string>> newPermutationsResult = new List<List<string>>();
            foreach (List<string> priorPermutation in priorPermutations)
            {
                foreach (string addition in additions)
                {
                    List<string> priorWithAddition = new List<string>(priorPermutation);
                    priorWithAddition.Add(addition);
                    newPermutationsResult.Add(priorWithAddition);
                }
            }
            return newPermutationsResult;
        }

答案 5 :(得分:2)

您要求的是笛卡尔积。一旦你知道了它的名字,Stack Overflow就会有几个类似的问题。他们似乎最终都指向一个答案,最终写成博客文章:

http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx

答案 6 :(得分:1)

非递归解决方案:

foreach (String s1 in array1) {
    foreach (String s2 in array2) {
        foreach (String s3 in array3) {
            String result = s1 + " " + s2 + " " + s3;
            //do something with the result
        }
    }
}

递归解决方案:

private ArrayList<String> permute(ArrayList<ArrayList<String>> ar, int startIndex) {
    if (ar.Count == 1) {
        foreach(String s in ar.Value(0)) {
            ar.Value(0) = "val" + startIndex + "=" + ar.Value(0);
        return ar.Value(0);
    }
    ArrayList<String> ret = new ArrayList<String>();
    ArrayList<String> tmp1 ar.Value(0);
    ar.remove(0);
    ArrayList<String> tmp2 = permute(ar, startIndex+1);
    foreach (String s in tmp1) {
        foreach (String s2 in tmp2) {
            ret.Add("val" + startIndex + "=" + s + " " + s2);
        }
    }
    return ret;
}

答案 7 :(得分:0)

这是我编写的通用递归函数(以及可能方便调用的重载):

Public Shared Function GetCombinationsFromIEnumerables(ByRef chain() As Object, ByRef IEnumerables As IEnumerable(Of IEnumerable(Of Object))) As List(Of Object())
    Dim Combinations As New List(Of Object())
    If IEnumerables.Any Then
        For Each v In IEnumerables.First
            Combinations.AddRange(GetCombinationsFromIEnumerables(chain.Concat(New Object() {v}).ToArray, IEnumerables.Skip(1)).ToArray)
        Next
    Else
        Combinations.Add(chain)
    End If
    Return Combinations
End Function

Public Shared Function GetCombinationsFromIEnumerables(ByVal ParamArray IEnumerables() As IEnumerable(Of Object)) As List(Of Object())
    Return GetCombinationsFromIEnumerables(chain:=New Object() {}, IEnumerables:=IEnumerables.AsEnumerable)
End Function

C#中的等价物:

public static List<object[]> GetCombinationsFromIEnumerables(ref object[] chain, ref IEnumerable<IEnumerable<object>> IEnumerables)
{
List<object[]> Combinations = new List<object[]>();
if (IEnumerables.Any) {
    foreach ( v in IEnumerables.First) {
        Combinations.AddRange(GetCombinationsFromIEnumerables(chain.Concat(new object[] { v }).ToArray, IEnumerables.Skip(1)).ToArray);
    }
} else {
    Combinations.Add(chain);
}
return Combinations;
}

public static List<object[]> GetCombinationsFromIEnumerables(params IEnumerable<object>[] IEnumerables)
{
return GetCombinationsFromIEnumerables(chain = new object[], IEnumerables = IEnumerables.AsEnumerable);
}

易于使用:

Dim list1 = New String() {"hello", "bonjour", "hallo", "hola"}
Dim list2 = New String() {"Erwin", "Larry", "Bill"}
Dim list3 = New String() {"!", ".."}
Dim result = MyLib.GetCombinationsFromIEnumerables(list1, list2, list3)
For Each r In result
    Debug.Print(String.Join(" "c, r))
Next

或在C#中:

object list1 = new string[] {"hello","bonjour","hallo","hola"};
object list2 = new string[] {"Erwin", "Larry", "Bill"};
object list3 = new string[] {"!",".."};
object result = MyLib.GetCombinationsFromIEnumerables(list1, list2, list3);
foreach (r in result) {
Debug.Print(string.Join(' ', r));
}

答案 8 :(得分:0)

这是一个使用非常少的代码的版本,完全是声明性的

    public static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> collection) where T : IComparable
    {
        if (!collection.Any())
        {
            return new List<IEnumerable<T>>() {Enumerable.Empty<T>() };
        }
        var sequence = collection.OrderBy(s => s).ToArray();
        return sequence.SelectMany(s => GetPermutations(sequence.Where(s2 => !s2.Equals(s))).Select(sq => (new T[] {s}).Concat(sq)));
    }

答案 9 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        var listofInts = new List<List<int>>(3);
        listofInts.Add(new List<int>{1, 2, 3});
        listofInts.Add(new List<int> { 4,5,6 });
        listofInts.Add(new List<int> { 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<List<T>> CrossJoinLists<T>(IEnumerable<List<T>> listofObjects)
    {
        var result = from obj in listofObjects.First()
                     select new List<T> {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<T>(obj){ obj2 };
        }

        return result;
    }
}

答案 10 :(得分:0)

这是一个非递归的非Linq解决方案。我不禁觉得我可以减少循环并用除法和模数计算位置,但不能完全绕过它。

static void Main(string[] args)
    {
        //build test list
        List<string[]> myList = new List<string[]>();
        myList.Add(new string[0]);
        myList.Add(new string[0]);
        myList.Add(new string[0]);
        myList[0] = new string[] { "1", "2", "3"};
        myList[1] = new string[] { "4", "5" };
        myList[2] = new string[] { "7", "8", "9" };

        object[][] xProds = GetProducts(myList.ToArray());
        foreach(object[] os in xProds)
        {
            foreach(object o in os)
            {
                Console.Write(o.ToString() + " ");
            }
            Console.WriteLine();
        }
        Console.ReadKey();
    }

    static object[][] GetProducts(object[][] jaggedArray){
        int numLists = jaggedArray.Length;
        int nProducts = 1;
        foreach (object[] oArray in jaggedArray)
        {
            nProducts *= oArray.Length;
        }
        object[][] productAry = new object[nProducts][];//holds the results
        int[] listIdxArray = new int[numLists];
        listIdxArray.Initialize();
        int listPtr = 0;//point to current list

        for(int rowcounter = 0; rowcounter < nProducts; rowcounter++)
        {
            //create a result row
            object[] prodRow = new object[numLists];
            //get values for each column
            for(int i=0;i<numLists;i++)
            {
                prodRow[i] = jaggedArray[i][listIdxArray[i]];
            }
            productAry[rowcounter] = prodRow;
            //move the list pointer
            //possible states
            // 1) in a list, has room to move down
            // 2) at bottom of list, can move to next list
            // 3) at bottom of list, no more lists left
            //in a list, can move down
            if (listIdxArray[listPtr] < (jaggedArray[listPtr].Length - 1))
            {
                listIdxArray[listPtr]++;
            }
            else
            {
                //can move to next column?
                //move the pointer over until we find a list, or run out of room
                while (listPtr < numLists && listIdxArray[listPtr] >= (jaggedArray[listPtr].Length - 1))
                {
                    listPtr++;
                }
                if (listPtr < listIdxArray.Length && listIdxArray[listPtr] < (jaggedArray[listPtr].Length - 1))
                {
                    //zero out the previous stuff
                    for (int k = 0; k < listPtr; k++)
                    {
                        listIdxArray[k] = 0;
                    }
                    listIdxArray[listPtr]++;
                    listPtr = 0;
                }
            }
        }
        return productAry;
    }

答案 11 :(得分:0)

当我为大量代码执行此操作时,我所遇到的一个问题是,在给出brian的示例时,我实际上已经耗尽了内存。为了解决这个问题,我使用了以下代码。

static void foo(string s, List<Array> 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<Array> a = new List<Array>();
        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();
    }

答案 12 :(得分:0)

private static void GetP(List<List<string>> conditions, List<List<string>> combinations, List<string> conditionCombo, List<string> previousT, int selectCnt)
{
    for (int i = 0; i < conditions.Count(); i++)
    {
        List<string> oneField = conditions[i];
        for (int k = 0; k < oneField.Count(); k++)
        {
            List<string> t = new List<string>(conditionCombo);
            t.AddRange(previousT);
            t.Add(oneField[k]);

            if (selectCnt == t.Count )
            {
                combinations.Add(t);
                continue;
            }
            GetP(conditions.GetRange(i + 1, conditions.Count - 1 - i), combinations, conditionCombo, t, selectCnt);
        }

    }
}

List<List<string>> a = new List<List<string>>();
a.Add(new List<string> { "1", "5", "3", "9" });
a.Add(new List<string> { "2", "3" });
a.Add(new List<string> { "93" });
List<List<string>> result = new List<List<string>>();
GetP(a, result, new List<string>(), new List<string>(), a.Count);

另一个递归函数。