在此代码keepSelectedFiles
中包含5个文本文件。 keepFiles
包含来自keepSelectedFiles
的3个文本文件。我只想比较这两个数组,然后从keepFiles
中删除3 keepSelectedFiiles
。然后在keepSelectedFiles
中只剩下2个文件。此代码无效。
for (int i = 0; i < keepSelectedFiles.GetSize(); i++)
{
testbox4.AddString(keepSelectedFiles[i]);
CString s1 = keepSelectedFiles[i];
CString s2 = keepFiles[i];
for (int i = 0; i < keepFiles.GetSize(); i++)
{
if (s1.Compare(s2) != 0)
{
junkFiles.Add(keepSelectedFiles[i]);
}
}
}
任何人都知道如何解决这个问题?
答案 0 :(得分:1)
您可以尝试此代码
for (int i = 0; i < keepSelectedFiles.GetSize(); i++)
{
bool flag = false;
CString s1 = keepSelectedFiles[i];
for (int j = 0; j < keepFiles.GetSize(); j++)// you can use different variable name
{ //so there will be no confusion
CString s2 = keepFiles[j];
if (s1.Compare(s2) == 0) // if s1 equal to s2
{
flag = true;
break;
}
}
if(!flag){
junkFiles.Add(keepSelectedFiles[i]); // or you can pass s1 here
keepSelectedFile.RemoveAt(i); // remove index value
i--;
}
else // first empty textbox4 before this loop
testbox4.AddString(keepSelectedFiles[i]); // update textbox value
}
答案 1 :(得分:0)
CString s2 = keepFiles[i];
移动上面的行并将此行放在内部for循环中。
答案 2 :(得分:0)
你不需要2个for循环:
for (int i = 0, j=0; i < keepSelectedFiles.GetSize(); i++)
{
CString s1 = keepSelectedFiles[i];
CString s2 = keepFiles[j];
if (s1.Compare(s2) != 0)
{
junkFiles.Add(keepSelectedFiles[i]);
}
else
{
++j;
testbox4.AddString(keepSelectedFiles[i]);
}
}