C# 比较 4 个字符串数组
我正在尝试比较四个数组。四个数组中的两个存储文件名,另外两个数组存储哈希码。
存储文件名的数组之一与存储哈希码的数组之一相关,这些哈希码是针对文件名数组中的文件的。
例如,我有两个项目 ProjectOne
和 ProjectTwo
。 ProjectOne
是一个名为 ProjectOneFiles
的数组和一个 ProjectOneHashcodes
数组的组合。这两个数组的长度始终相同。
ProjectTwo
将是一个名为 ProjectTwoFiles
的数组和一个 ProjectTwoHashcodes
数组的组合。这两个数组也将始终具有相同的长度。
但是,ProjectOne
不一定与 ProjectTwo
长度相同。这就是我目前所拥有的:
private void Compare()
{
String[] ProjectOneFiles = ProjOneFiles();
String[] ProjectTwoFiles = ProjTwoFiles();
String[] ProjectOneHash = ProjOneHash();
String[] ProjectTwoHash = ProjTwoHash();
for (int x = 0; x < ProjectOneFiles.length; ++x)
{
String Test1 = ProjectOneFiles[x];
String Test2 = ProjectTwoFiles[x];
String Test3 = ProjectOneHash[x];
String Test4 = ProjectTwoHash[x];
if (Test1 != Test2)
{
listBox6.Items.Add(Test1);
listBox6.Items.Add(Test2);
}
else if ((Test1 == Test2) && (Test3 == Test4))
{
listBox7.Items.Add(Test1);
}
else
{
listBox8.Items.Add(Test1);
}
}
}
显然现在我的问题出现在四个数组中的两个长度不一样的地方。如果我尝试迭代并且使用最小的数组大小,因此我不会完全迭代最大的数组。
如果我尝试迭代一个数组,比如说,有五个项目,一百次,显然我会得到一个超出范围的异常。
我需要首先比较两个文件名数组,然后如果它们相同则比较两个哈希码数组。如果它们满足某些要求,则需要将它们放入三个列表之一,并且所有三个列表都可能被填充。
我本来希望在 LINQ 中完成此操作:
var onlyinfirst =
from s in ProjectOneFiles
where !ProjectTwoFiles.Contains(s) &&
(ProjectOneHash.Contains(p) == ProjectTwoHash.Contains(p))
select s;
var onlyinsecond =
from s in ProjectTwoFiles
where !ProjectOneFiles.Contains(s)
select s;
var onboth =
from s in ProjectOneFiles
where ProjectTwoFiles.Contains(s)
select s;
但是我一直坚持这样做,因为我不确定如何执行嵌套语句来删除我的 if
语句。正如你在我的第一行中看到的,我试图这样做,但我现在完全坚持使用它。文件名数组和哈希码数组都是相关的,因此 LINQ 语句必须在一次传递中,否则哈希码将不再与文件名关联(索引方式)。
我的很多问题可以通过使用多维数组来解决,但我发现它们非常令人困惑,我宁愿不必重写我的函数来使用它们。
帮助解释的额外信息和函数:
如果有人需要这些信息来帮助回答我的问题,文件名和数组将从 XML 文档中提取,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Projects>
<Project Name="tfasdtyf">
<TestCycle Number="2387468">
<Files>
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest1.dll" />
<HashCode Code="0E-C5-03-AD-CC-21-62-49-D9-36-3F-C4-F1-17-BC-11" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
<HashCode Code="60-46-A3-6F-82-E4-0A-00-2A-60-83-47-B2-16-F3-25" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
<HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
</Files>
</TestCycle>
</Project>
<Project Name="tfasdtyf">
<TestCycle Number="23423">
<Files>
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest.dll" />
<HashCode Code="0E-C5-03-AD-CC-21-62-49-D9-36-3F-C4-F1-17-BC-11" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
<HashCode Code="60-46-A3-6F-82-E4-0A-00-2A-60-83-47-B2-16-F3-24" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
<HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
</Files>
</TestCycle>
</Project>
<Project Name="Music">
<TestCycle Number="12312">
<Files>
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Shut Ya Mouth.mp3" />
<HashCode Code="3E-92-80-93-D5-64-19-16-26-8D-39-2A-C7-0B-C8-EB" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Snake Eater.mp3" />
<HashCode Code="8B-DF-19-AE-87-52-64-2E-85-CF-57-4B-85-4D-CC-E9" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Stuck in the System.mp3" />
<HashCode Code="6A-30-A7-53-FF-29-A5-DF-6D-24-DF-41-74-EE-06-4D" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 Martin Solveig - Hello (Featuring Dragonette).mp3" />
<HashCode Code="93-90-A3-9C-BE-81-63-03-D7-96-1F-72-E4-ED-2D-32" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 Stimming - Funkworm.mp3" />
<HashCode Code="8F-E1-7A-F1-B7-80-C6-2F-DC-34-FD-82-A0-DA-35-5E" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\1. Downlink - Ignition.mp3" />
<HashCode Code="3D-89-B3-C2-73-A6-A0-85-02-C0-B4-F9-C8-09-14-C7" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02 B1 Psychedelic Runway.mp3" />
<HashCode Code="00-72-5C-CE-25-73-98-31-69-71-68-48-31-A1-A3-5A" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02 B1 Rapture.mp3" />
<HashCode Code="1E-A6-53-07-10-FD-A3-4C-EF-D6-92-7F-CE-97-88-6E" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02_Digital-Controller.mp3" />
<HashCode Code="94-E0-CA-5F-2B-D2-56-7B-AF-2E-04-50-58-38-4D-B4" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\2. Downlink - Gamma Ray.mp3" />
<HashCode Code="3C-7A-76-AD-A6-2C-D1-7E-61-24-C0-40-BD-A7-A9-41" />
</Files>
</TestCycle>
</Project>
</Projects>
然后我有四个与下面类似的函数提取项目的文件名和哈希码,这就是我获取每个数组的方式:
private String[] ProjOneFiles()
{
//Intialize the functions in the DLL
DllTest.Funtions Functions = new DllTest.Funtions();
//Set the location where the XMLs can be found
String Directory = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\";
//Get and set the number of items in the directory
int NumFiles = Functions.GetNumFiles(Directory);
//Create a search string to be used to determine the fullpath name of the file
//selected from the combobox
String SelectedFile = comboBox1.SelectedItem.ToString();
String SearchString = "*" + SelectedFile + "*.XML";
//Get and set the TC that will be used to get the filenames and hashcodes
int SelectedTC = int.Parse(comboBox2.SelectedItem.ToString());
//Get and set an array containing a full path structure to the item selected from
//the combobox using the search string created earlier. Get files returns an array
//thus needs to be stored in an array
String[] FullPaths = new String[NumFiles];
FullPaths = System.IO.Directory.GetFiles(
"C:\\Users\\brandonm\\Desktop\\Backup\\XML",
SearchString,
System.IO.SearchOption.AllDirectories);
int number = FullPaths.GetLength(0);
// The number of items in the XML ie. Number of Filenames in a particular TC
int NumXMLItems = NumXMLItemsListOne();
// Initialize the array that will hold the Filenames and their equivalent Hashcodes
String[] FileNames = new String[NumXMLItems];
int y = 0;
//Itteration through the all the XMLs in the location to add the current items into their arrays
for (int x = 0; x < NumFiles; x++)
{
String FullPath = FullPaths[x];
XPathNavigator Root = new XPathDocument(FullPath).CreateNavigator();
foreach (XPathNavigator Cycle in Root.Select(
String.Format(@"//TestCycle[@Number = '{0}']",
SelectedTC))
)
{
foreach (XPathNavigator Nav in Cycle.Select(@"Files/FileName/@File"))
{
FileNames[y] = Nav.Value;
//listBox4.Items.Add(Nav.Value);
y = y + 1;
}
}
}
return FileNames;
}
private String[] ProjOneHash()
{
//Intialize the functions in the DLL
DllTest.Funtions Functions = new DllTest.Funtions();
//Set the location where the XMLs can be found
String Directory = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\";
//Get and set the number of items in the directory
int NumFiles = Functions.GetNumFiles(Directory);
//Create a search string to be used to determine the fullpath name of the file
//selected from the combobox
String SelectedFile = comboBox1.SelectedItem.ToString();
String SearchString = "*" + SelectedFile + "*.XML";
//Get and set the TC that will be used to get the filenames and hashcodes
int SelectedTC = int.Parse(comboBox2.SelectedItem.ToString());
//Get and set an array containing a full path structure to the item selected from
//the combobox using the search string created earlier. Get files returns an array
//thus needs to be stored in an array
String[] FullPaths = new String[NumFiles];
FullPaths = System.IO.Directory.GetFiles(
"C:\\Users\\brandonm\\Desktop\\Backup\\XML",
SearchString,
System.IO.SearchOption.AllDirectories);
int number = FullPaths.GetLength(0);
// The number of items in the XML ie. Number of Filenames in a particular TC
int NumXMLItems = NumXMLItemsListOne();
// Initialize the array that will hold the Filenames and their equivalent Hashcodes
String[] HashCode = new String[NumXMLItems];
int z = 0;
//Itteration through the all the XMLs in the location to add the current items into their arrays
for (int x = 0; x < NumFiles; x++)
{
String FullPath = FullPaths[x];
XPathNavigator Root = new XPathDocument(FullPath).CreateNavigator();
foreach (XPathNavigator Cycle in Root.Select(
String.Format(@"//TestCycle[@Number = '{0}']",
SelectedTC))
)
{
foreach (XPathNavigator Nav in Cycle.Select(@"Files/HashCode/@Code"))
{
HashCode[z] = Nav.Value;
//listBox4.Items.Add(Nav.Value);
z = z + 1;
}
}
}
return HashCode;
}
I'm trying to compare four arrays. Two of the four arrays store filenames, while the other two arrays store hashcodes.
One of the arrays storing file names is related to one of the arrays storing hashcodes, these hashcodes are for the file in the file name array.
So for example I have two projects ProjectOne
and ProjectTwo
. ProjectOne
is a combination of an array called ProjectOneFiles
and an array of ProjectOneHashcodes
. Both those arrays will always be of the same length.
ProjectTwo
will be a combination of an array called ProjectTwoFiles
and an array of ProjectTwoHashcodes
. Both those arrays will also always be of the same length.
However ProjectOne
won't necessarily be the same length as ProjectTwo
. This is what I currently have:
private void Compare()
{
String[] ProjectOneFiles = ProjOneFiles();
String[] ProjectTwoFiles = ProjTwoFiles();
String[] ProjectOneHash = ProjOneHash();
String[] ProjectTwoHash = ProjTwoHash();
for (int x = 0; x < ProjectOneFiles.length; ++x)
{
String Test1 = ProjectOneFiles[x];
String Test2 = ProjectTwoFiles[x];
String Test3 = ProjectOneHash[x];
String Test4 = ProjectTwoHash[x];
if (Test1 != Test2)
{
listBox6.Items.Add(Test1);
listBox6.Items.Add(Test2);
}
else if ((Test1 == Test2) && (Test3 == Test4))
{
listBox7.Items.Add(Test1);
}
else
{
listBox8.Items.Add(Test1);
}
}
}
Obviously now my problem appears where I have two of the four arrays not being the same length. If I try to iterate through and I'm using the smallest array size therefore I wont iterate through the largest array completely.
If I try to iterate through an array which, lets say, has five items, one hundred times obviously I get an out of range exception.
I need to compare the two file name arrays first and then if they are the same compare the two hashcode arrays. If they meet certain requirements they need to be put in one of three lists and all three lists could possibly be filled.
I would have liked to have done it in LINQ:
var onlyinfirst =
from s in ProjectOneFiles
where !ProjectTwoFiles.Contains(s) &&
(ProjectOneHash.Contains(p) == ProjectTwoHash.Contains(p))
select s;
var onlyinsecond =
from s in ProjectTwoFiles
where !ProjectOneFiles.Contains(s)
select s;
var onboth =
from s in ProjectOneFiles
where ProjectTwoFiles.Contains(s)
select s;
However I got stuck doing it that way because I'm not sure how to do nested statements to remove my if
statements. As you can see in my first line I tried to do just that but im completely stuck using it now. Both the filename array and the hashcode array are related so the LINQ statement has to be in a single pass otherwise the hashcode will no longer correlate (index wise) to the file name.
A lot of my issues may be solved with using a multidimensional array but I find them very confusing and i would rather not have to go rewrite my functions to work with them.
Extra Info and functions to help explain:
In case someone needs this information to help answer my question, the filenames and arrays are pulled from an XML document like this:
<?xml version="1.0" encoding="utf-8"?>
<Projects>
<Project Name="tfasdtyf">
<TestCycle Number="2387468">
<Files>
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest1.dll" />
<HashCode Code="0E-C5-03-AD-CC-21-62-49-D9-36-3F-C4-F1-17-BC-11" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
<HashCode Code="60-46-A3-6F-82-E4-0A-00-2A-60-83-47-B2-16-F3-25" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
<HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
</Files>
</TestCycle>
</Project>
<Project Name="tfasdtyf">
<TestCycle Number="23423">
<Files>
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\DllTest.dll" />
<HashCode Code="0E-C5-03-AD-CC-21-62-49-D9-36-3F-C4-F1-17-BC-11" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.exe" />
<HashCode Code="60-46-A3-6F-82-E4-0A-00-2A-60-83-47-B2-16-F3-24" />
<FileName File="C:\Users\brandonm\Documents\Visual Studio 2008\Projects\WpfDllTest\WpfDllTest\bin\x86\Release\WpfDllTest.vshost.exe" />
<HashCode Code="76-7B-6F-37-0D-3A-F2-F4-32-D1-70-A5-75-3B-DE-95" />
</Files>
</TestCycle>
</Project>
<Project Name="Music">
<TestCycle Number="12312">
<Files>
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Shut Ya Mouth.mp3" />
<HashCode Code="3E-92-80-93-D5-64-19-16-26-8D-39-2A-C7-0B-C8-EB" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Snake Eater.mp3" />
<HashCode Code="8B-DF-19-AE-87-52-64-2E-85-CF-57-4B-85-4D-CC-E9" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 A1 Stuck in the System.mp3" />
<HashCode Code="6A-30-A7-53-FF-29-A5-DF-6D-24-DF-41-74-EE-06-4D" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 Martin Solveig - Hello (Featuring Dragonette).mp3" />
<HashCode Code="93-90-A3-9C-BE-81-63-03-D7-96-1F-72-E4-ED-2D-32" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\01 Stimming - Funkworm.mp3" />
<HashCode Code="8F-E1-7A-F1-B7-80-C6-2F-DC-34-FD-82-A0-DA-35-5E" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\1. Downlink - Ignition.mp3" />
<HashCode Code="3D-89-B3-C2-73-A6-A0-85-02-C0-B4-F9-C8-09-14-C7" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02 B1 Psychedelic Runway.mp3" />
<HashCode Code="00-72-5C-CE-25-73-98-31-69-71-68-48-31-A1-A3-5A" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02 B1 Rapture.mp3" />
<HashCode Code="1E-A6-53-07-10-FD-A3-4C-EF-D6-92-7F-CE-97-88-6E" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\02_Digital-Controller.mp3" />
<HashCode Code="94-E0-CA-5F-2B-D2-56-7B-AF-2E-04-50-58-38-4D-B4" />
<FileName File="C:\Users\brandonm\Desktop\Stuff\Dubstep\2. Downlink - Gamma Ray.mp3" />
<HashCode Code="3C-7A-76-AD-A6-2C-D1-7E-61-24-C0-40-BD-A7-A9-41" />
</Files>
</TestCycle>
</Project>
</Projects>
I then have four functions similar to the one below that pull the the filenames and hashcodes for the projects and that's how I get each of the arrays:
private String[] ProjOneFiles()
{
//Intialize the functions in the DLL
DllTest.Funtions Functions = new DllTest.Funtions();
//Set the location where the XMLs can be found
String Directory = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\";
//Get and set the number of items in the directory
int NumFiles = Functions.GetNumFiles(Directory);
//Create a search string to be used to determine the fullpath name of the file
//selected from the combobox
String SelectedFile = comboBox1.SelectedItem.ToString();
String SearchString = "*" + SelectedFile + "*.XML";
//Get and set the TC that will be used to get the filenames and hashcodes
int SelectedTC = int.Parse(comboBox2.SelectedItem.ToString());
//Get and set an array containing a full path structure to the item selected from
//the combobox using the search string created earlier. Get files returns an array
//thus needs to be stored in an array
String[] FullPaths = new String[NumFiles];
FullPaths = System.IO.Directory.GetFiles(
"C:\\Users\\brandonm\\Desktop\\Backup\\XML",
SearchString,
System.IO.SearchOption.AllDirectories);
int number = FullPaths.GetLength(0);
// The number of items in the XML ie. Number of Filenames in a particular TC
int NumXMLItems = NumXMLItemsListOne();
// Initialize the array that will hold the Filenames and their equivalent Hashcodes
String[] FileNames = new String[NumXMLItems];
int y = 0;
//Itteration through the all the XMLs in the location to add the current items into their arrays
for (int x = 0; x < NumFiles; x++)
{
String FullPath = FullPaths[x];
XPathNavigator Root = new XPathDocument(FullPath).CreateNavigator();
foreach (XPathNavigator Cycle in Root.Select(
String.Format(@"//TestCycle[@Number = '{0}']",
SelectedTC))
)
{
foreach (XPathNavigator Nav in Cycle.Select(@"Files/FileName/@File"))
{
FileNames[y] = Nav.Value;
//listBox4.Items.Add(Nav.Value);
y = y + 1;
}
}
}
return FileNames;
}
private String[] ProjOneHash()
{
//Intialize the functions in the DLL
DllTest.Funtions Functions = new DllTest.Funtions();
//Set the location where the XMLs can be found
String Directory = "C:\\Users\\brandonm\\Desktop\\Backup\\XML\\";
//Get and set the number of items in the directory
int NumFiles = Functions.GetNumFiles(Directory);
//Create a search string to be used to determine the fullpath name of the file
//selected from the combobox
String SelectedFile = comboBox1.SelectedItem.ToString();
String SearchString = "*" + SelectedFile + "*.XML";
//Get and set the TC that will be used to get the filenames and hashcodes
int SelectedTC = int.Parse(comboBox2.SelectedItem.ToString());
//Get and set an array containing a full path structure to the item selected from
//the combobox using the search string created earlier. Get files returns an array
//thus needs to be stored in an array
String[] FullPaths = new String[NumFiles];
FullPaths = System.IO.Directory.GetFiles(
"C:\\Users\\brandonm\\Desktop\\Backup\\XML",
SearchString,
System.IO.SearchOption.AllDirectories);
int number = FullPaths.GetLength(0);
// The number of items in the XML ie. Number of Filenames in a particular TC
int NumXMLItems = NumXMLItemsListOne();
// Initialize the array that will hold the Filenames and their equivalent Hashcodes
String[] HashCode = new String[NumXMLItems];
int z = 0;
//Itteration through the all the XMLs in the location to add the current items into their arrays
for (int x = 0; x < NumFiles; x++)
{
String FullPath = FullPaths[x];
XPathNavigator Root = new XPathDocument(FullPath).CreateNavigator();
foreach (XPathNavigator Cycle in Root.Select(
String.Format(@"//TestCycle[@Number = '{0}']",
SelectedTC))
)
{
foreach (XPathNavigator Nav in Cycle.Select(@"Files/HashCode/@Code"))
{
HashCode[z] = Nav.Value;
//listBox4.Items.Add(Nav.Value);
z = z + 1;
}
}
}
return HashCode;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单但不是特别优雅的方式:
The simple but not particularly elegant way:
为了详细说明上面提到的字典方法,您可能会考虑以下内容:
字典 hashDic = new Dictionary();
To elaborate on the Dictionary approach mentioned above, you might consider something like:
Dictionary hashDic = new Dictionary();
诚然,我在最后跳过了一些内容,但这对你有用吗?
我不确定您到底在哪里陷入困境,但似乎您只需要帮忙找出每个列表中哪些项目是唯一的,哪些项目不是唯一的。
在这种情况下,这应该会有所帮助:
我使用“var”关键字来节省输入,但将从 LINQ 语句中吐出的实际数据类型是 IEnumerable。如果你需要它是一个列表,你可以在行尾调用 .ToList() ,如下所示:
一般来说,尽管 ToList() 不是地球上最快的方法,所以虽然有时有必要,如果你真的不需要 List 对象,我会避免它。
.Except(...) 仅获取一个列表中的所有字符串,并排除另一个列表中的任何字符串。
.Intersect(...) 仅查找两个列表中的文件。
如果我还遗漏了其他内容,请在评论中告诉我,我会看看是否可以提供帮助。
Admittedly, I skipped over some of the stuff at the end, but how does this work for you?
I'm not sure precisely where you're getting stuck, but it seems like you just need a hand figuring out which items are unique and which items are not unique to each list.
In that case, this should help:
I used the "var" keyword to save typing, but the actual data type that is going to get spit out of the LINQ statement is IEnumerable. If you need it to be a List you can just call .ToList() at the end of the line, like so:
Generally speaking, though--ToList() isn't the fastest method on the planet so while its sometimes necessary, if you really don't need List objects, I'd avoid it.
.Except(...) just takes all of the strings in one list and excludes any of them that are also in the other list.
.Intersect(...) finds only the files that are in both lists.
If there's something else in particular that I missed, let me know in a comment and I'll see if I can help out.
试试这个。
诀窍是通过交换使 ProjectOne 成为更大的数组。现在所有操作都很简单,因为您始终可以预测更大的数组。
免责声明:这只是解决您的问题。它根本没有优化。
可以从将
DevelopSomalia()
更改为DevelopCountry("Somalia")
开始优化Try this.
The trick is to make ProjectOne the bigger array by swapping. Now all operations are easy as you can always predict the bigger array.
Disclaimer: This just solves your issue. It is not optimised at all.
Could start optimising from changing
DevelopSomalia()
toDevelopCountry("Somalia")