c# – 当我处理多种数据类型时,如何处理多个foreach循环
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 当我处理多种数据类型时,如何处理多个foreach循环,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4167字,纯文字阅读大概需要6分钟。
内容图文
![c# – 当我处理多种数据类型时,如何处理多个foreach循环](/upload/InfoBanner/zyjiaocheng/805/718d965006934e65a040bba6d5f7bdbd.jpg)
我有一个困扰我一段时间的问题,它关系到我程序中循环的增长呈指数级增长.我将让下面的代码进行讨论并在其中添加注释.
void Main()
{
//Here we are just creating simple lists
List<string> strings = new List<string>();
strings.Add("a");
strings.Add("b");
strings.Add("c");
List<int> integers = new List<int>();
integers.Add(1);
integers.Add(2);
integers.Add(3);
//Creating complex classes ( not really )
ComplexClass cc1 = new ComplexClass();
cc1.CCString = "A test";
cc1.CCInt = 2;
ComplexClass cc2 = new ComplexClass();
cc2.CCString = "Another test";
cc2.CCInt = 6;
//Creating a list of these too
List< ComplexClass > complexClasses = new List< ComplexClass >();
complexClasses.Add(cc1);
complexClasses.Add(cc2);
//Here i want to create every possible combination using each of the lists
//and then add these to a testData class to do other things with, serialize, save, print etc.
//The main question is here, the for loops will definitely increase exponentially with each
//list added to.
foreach( int i in integers )
{
foreach( string s in strings )
{
foreach( ComplexClass compClass in complexClasses )
{
TestData data = new TestData();
data.TestInteger = i;
data.TestString = s;
data.TestComplexClass = compClass;
OutPutTestData( data );
}
}
}
}
//Simply outputs the data as test but I will be keeping the object for later also
public void OutPutTestData( TestData testData )
{
Console.WriteLine( testData.TestString + testData.TestInteger + testData.TestComplexClass.CCString );
}
//The "Complex class" again not that complex but an example of what im tring to achieve
public class ComplexClass
{
public string CCString{ get; set; }
public int CCInt { get; set; }
}
//The overall test object which holds multiple properties of different data types
public class TestData
{
public string TestString { get; set; }
public int TestInteger { get; set; }
public ComplexClass TestComplexClass { get; set; }
}
产量
a1 Test1
a1 Test2
b1 Test1
b1 Test2
c1 Test1
c1 Test2
a2 Test1
a2 Test2
b2 Test1
b2 Test2
c2 Test1
c2 Test2
a3 Test1
a3 Test2
b3 Test1
b3 Test2
c3 Test1
c3 Test2
正如您所看到的,循环工作并为我提供所提供数据的所有可能组合.
我的问题是for循环的指数式增长,因为我添加了更多列表.可能有大量的列表.
我确实理解迭代次数会随着组合的发现而增加,这不是问题,因为我计划在估算可能的总迭代次数后,以编程方式限制基于用户输入可能发生的迭代次数.
例如总迭代次数为234次,因此只迭代120次(120次组合)
提供的代码适用于嵌套的foreach循环,但随着它呈指数级增长,它变得难以阅读,难以管理并且通常难看.
我已经看过像这样的Permutation算法:
Algorithm to generate all possible permutations of a list?
Understanding Recursion to generate permutations.
但是它们只允许使用一种特定的数据类型而不是多种数据类型.
我也研究了笛卡尔积,但我发现的唯一例子只涉及一种数据类型.
解决方法:
即使你选择了一个答案,我想你可能想看一下这个…通过使用递归,你所要做的就是将你的所有列表放在List< IList>中.您只需将所有新添加的列表添加到List< IList>中即可.
我为您的ComplexClass添加了一个覆盖ToString(),以使其适合.
public static void Test()
{
//Here we are just creating simple lists
List<string> strings = new List<string>();
strings.Add("a");
strings.Add("b");
strings.Add("c");
List<int> integers = new List<int>();
integers.Add(1);
integers.Add(2);
integers.Add(3);
//Creating complex classes ( not really )
ComplexClass cc1 = new ComplexClass();
cc1.CCString = "A test";
cc1.CCInt = 2;
ComplexClass cc2 = new ComplexClass();
cc2.CCString = "Another test";
cc2.CCInt = 6;
//Creating a list of these too
List<ComplexClass> complexClasses = new List<ComplexClass>();
complexClasses.Add(cc1);
complexClasses.Add(cc2);
// NEW LIST
List<double> doubles = new List<double>();
doubles.Add(99.99);
doubles.Add(100.12);
List<IList> myLists = new List<IList> {integers, strings, complexClasses, doubles};
Permutate("", myLists, 0);
Console.ReadLine();
}
public static void Permutate(string s, List<IList> list, int i)
{
if (i == list.Count)
{
Console.WriteLine(s);
}
else
{
foreach (object obj in list[i])
{
Permutate(s + obj + " ", list, i + 1);
}
}
}
//The "Complex class" again not that complex but an example of what im tring to achieve
public class ComplexClass
{
public string CCString { get; set; }
public int CCInt { get; set; }
// Added override
public override string ToString()
{
return CCString + CCInt;
}
}
结果(并非所有结果都被捕获):
内容总结
以上是互联网集市为您收集整理的c# – 当我处理多种数据类型时,如何处理多个foreach循环全部内容,希望文章能够帮你解决c# – 当我处理多种数据类型时,如何处理多个foreach循环所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。