permutation of array items

前端 未结 4 2026
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 05:17

How do I go about getting a combination of an array of strings of count 2? Ie.

List myString = {\"a\", \"b\", \"c\", \"d\", \"f\"};
         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-14 06:04

    Without LINQ, you can use a nested loop:

    var permutations = new List();
    for(int i = 0; i < myString.Count; i++) 
    {
        for(int j = 0; j < myString.Count; j++)
        {
            if(i == j)
                continue;
    
            var permutation = string.Format("{0}{1}", myString[i], myString[j]);
            permutations.Add(permutation);
        }
    }
    

提交回复
热议问题