cは再帰を使用して、次のメソッドを実装します。私は再帰的なメソッドに以下の方法を変えることができる方法#
public List<ScanItem> search(List<ScanItem> allItems)
{
List<ScanItem> rt = new List<ScanItem>();
foreach (ScanItem s in allItems)
{
if (s.IsDirectory)
{
rt.Add(s);
foreach (ScanItem s1 in ((ScanDir)s).Items)
{
if (s1.IsDirectory)
{
rt.Add(s1);
foreach (ScanItem s2 in ((ScanDir)s1).Items)
{
if (s2.IsDirectory)
{
rt.Add(s2);
foreach (ScanItem s3 in ((ScanDir)s2).Items)
{
if (s3.IsDirectory)
{
rt.Add(s3);
/// repeat until there are no more direcotries
}
else
if (s3.Name.Contains("Foo"))
rt.Add(s3);
}
}
else
if (s2.Name.Contains("Foo"))
rt.Add(s2);
}
}
else
if (s1.Name.Contains("Foo"))
rt.Add(s1);
}
}
else
if (s.Name.Contains("Foo"))
rt.Add(s);
}
return rt;
}
は私が、私はそれを建てた方法をお見せしましょう:
ステップ1:
public List<ScanItem> search(List<ScanItem> allItems)
{
List<ScanItem> rt = new List<ScanItem>();
foreach (ScanItem s in allItems)
{
if (s.IsDirectory)
{
rt.Add(s);
}
else
if (s.Name.Contains("Foo"))
rt.Add(s);
}
return rt;
}
ステップ2:
public List<ScanItem> search(List<ScanItem> allItems)
{
List<ScanItem> rt = new List<ScanItem>();
foreach (ScanItem s in allItems)
{
if (s.IsDirectory)
{
rt.Add(s);
foreach (ScanItem s1 in ((ScanDir)s).Items)
{
if (s1.IsDirectory)
{
rt.Add(s1);
}
else
if (s1.Name.Contains("Foo"))
rt.Add(s1);
}
}
else
if (s.Name.Contains("Foo"))
rt.Add(s);
}
return rt;
}
ステップ3:私はちょうどコピーを保つ
public List<ScanItem> search(List<ScanItem> allItems)
{
List<ScanItem> rt = new List<ScanItem>();
foreach (ScanItem s in allItems)
{
if (s.IsDirectory)
{
rt.Add(s);
foreach (ScanItem s1 in ((ScanDir)s).Items)
{
if (s1.IsDirectory)
{
rt.Add(s1);
foreach (ScanItem s2 in ((ScanDir)s1).Items)
{
if (s2.IsDirectory)
{
rt.Add(s2);
foreach (ScanItem s3 in ((ScanDir)s2).Items)
{
if (s3.IsDirectory)
{
rt.Add(s3);
}
else
if (s3.Name.Contains("Foo"))
rt.Add(s3);
}
}
else
if (s2.Name.Contains("Foo"))
rt.Add(s2);
}
}
else
if (s1.Name.Contains("Foo"))
rt.Add(s1);
}
}
else
if (s.Name.Contains("Foo"))
rt.Add(s);
}
return rt;
}
注:4
public List<ScanItem> search(List<ScanItem> allItems)
{
List<ScanItem> rt = new List<ScanItem>();
foreach (ScanItem s in allItems)
{
if (s.IsDirectory)
{
rt.Add(s);
foreach (ScanItem s1 in ((ScanDir)s).Items)
{
if (s1.IsDirectory)
{
rt.Add(s1);
foreach (ScanItem s2 in ((ScanDir)s1).Items)
{
if (s2.IsDirectory)
{
rt.Add(s2);
}
else
if (s2.Name.Contains("Foo"))
rt.Add(s2);
}
}
else
if (s1.Name.Contains("Foo"))
rt.Add(s1);
}
}
else
if (s.Name.Contains("Foo"))
rt.Add(s);
}
return rt;
}
ステップ
foreach (ScanItem s3 in ((ScanDir)s2).Items)
{
if (s3.IsDirectory)
{
rt.Add(s3);
/// repeat until there are no more direcotries
}
else
if (s3.Name.Contains("Foo"))
rt.Add(s3);
}