嗨,我已经有一个类似的问题,但是不幸的是我无法弄清楚如何解决它。我有一个List<int> tmpList
和一个List<int> tmpSpeicherListe
,我想在其中复制我的tmpList
,以便可以编辑我的tmpList
并在需要时重置它。
我想在for循环中多次编辑tmpList
并在每次循环重复时将其重置,但是如果我在位置x处删除tmpList
的一个值,它也会在位置x处也删除tmpSpeicherListe的值。那么我应该在哪里声明List<int> tmpSpeicherListe = tmpList;
的声明,或者在哪里出错呢?
ps:这只是我的代码的一部分
//...
List<int> tmpSpeicherListe = tmpList; //copy tmpList
int basisPosition = 2;
int counter2 = 0;
while(counter2 + 2 < tmpList.Count - 1)
{
basis = tmpList[basisPosition];
for (int m = 1; m < tmpList.Count - basisPosition; m++)
{
int speicher = tmpList[basisPosition + m];
bool vorhanden = false;
for (int n = 0; n < intKombinierbar[basis].Length; n++)
{
if (intKombinierbar[basis][n] == speicher)
{
vorhanden = true;
break;
}
}
if(vorhanden == false)
{
tmpList.RemoveAt(basisPosition + m); //edit tmpList BUT tmpSpeicherListe changes too
}
}
if(tmpList.Count <= i)
{
tmpList = tmpSpeicherListe; //reset tmpList
counter2++;
basisPosition = 2 + counter2;
}
else if (basisPosition == tmpList.Count - 2)
{
großeKombinationen.Add(tmpList);
tmpList = tmpSpeicherListe; //reset tmpList
counter2++;
basisPosition = 2 + counter2;
}
else
{
basisPosition++;
}
}
//...
答案 0 :(得分:0)
您应该克隆List
。您将tmpSpeicherListe
的引用设置为tmpList
的引用。您可以像这样克隆。
List<int> tmpSpeicherListe = new List<int>(tmpList);