问题描述
我有一个 ArrayList[] myList 并且我正在尝试创建一个包含数组中值的所有排列的列表.
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)
myList[0] = { "1", "5", "3", "9" };
myList[1] = { "2", "3" };
myList[2] = { "93" };
myList 的数量可以变化,因此它的长度是事先未知的.
The count of myList can be varied so its length is not known beforehand.
我希望能够生成与以下类似的所有排列的列表(但有一些额外的格式).
I would like to be able to generate a list of all the permutations similar to the following (but with some additional formatting).
1 2 93
1 3 93
5 2 93
5 3 93
3 2 93
3 3 93
9 2 93
9 3 93
这对我想要完成的事情有意义吗?我似乎想不出一个好的方法来做到这一点(如果有的话).
Does this make sense of what I am trying to accomplish? I can't seem to come up with a good method for doing this, (if any).
我不确定递归是否会干扰我以自己的方式格式化输出的愿望.抱歉,我之前没有提到我的格式是什么.
I am not sure if recursion would interfere with my desire to format the output in my own manner. Sorry I did not mention before what my formatting was.
我想最终构建一个 string[] 数组,其中包含所有遵循以下格式的组合:
I want to end up building a string[] array of all the combinations that follows the format like below:
对于1 2 93"排列
for the "1 2 93" permutation
我希望输出为val0=1;val1=2;val2=93;"
I want the output to be "val0=1;val1=2;val2=93;"
我现在将尝试递归.谢谢Jokepu医生
I will experiment with recursion for now. Thank you DrJokepu
推荐答案
我很惊讶没有人发布 LINQ 解决方案.
I'm surprised nobody posted the LINQ solution.
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)
这篇关于数组列表的 C# 排列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!