[英]c# System.OutOfMemoryException How can I solve it?
嗨,當我嘗試計算超過24個元素的組合時,我使用此函數來計算對象“ Ricerca”列表的所有可能組合,但出現System.OutOfMemory異常。 有什么辦法可以解決這個問題?
這是我使用的功能:
private static List<List<Ricerca>> GetAllCombos(List<Ricerca> list)
{
int comboCount = (int)Math.Pow(2, list.Count) - 1;
List<List<Ricerca>> result = new List<List<Ricerca>>();
for (int i = 1; i < comboCount + 1; i++)
{
// make each combo here
result.Add(new List<Ricerca>());
for (int j = 0; j < list.Count; j++)
{
if ((i >> j) % 2 != 0)
result.Last().Add(list[j]);
}
}
return result;
}
謝謝你的幫助
您是否真的需要一次將它們全部保存在內存中?還是一次獲取每種組合就足夠了,以便您可以做點什么?
private static IEnumerable<List<Ricerca>> GetAllCombos(IReadOnlyCollection<Ricerca> list)
{
var comboCount = (int)Math.Pow(2, list.Count) - 1;
for (var i = 1; i < comboCount + 1; i++)
{
// make each combo here
yield return list.Where((t, j) => (i >> j) % 2 != 0).ToList();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.