正如 JB 所说,我认为这是一个两步解决方案,我已经在 C# 中尝试了一些基本的编码。如果你想在java中使用它,我想你一定知道如何使用它:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace So_Try1
{
class Program
{
public static int nthOccurrence(String str, char c, int n)
{
int pos = str.IndexOf(c, 0);
while (n-- > 0 && pos != -1)
pos = str.IndexOf(c, pos + 1);
return pos;
}
static void Main(string[] args)
{
List<String> pathString = new List<string>();
pathString.Add("C:\\XML");
pathString.Add("C:\\MyProg\\Raw");
pathString.Add("C:\\MyProg\\Subset\\MTSAT");
pathString.Add("C:\\MyProg\\Subset\\GOESW");
pathString.Add("D:\\Folder2\\Mosaic");
pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
pathString.Add("D:\\Dataset\\Composite");
pathString.Add("D:\\Dataset\\Global");
pathString.Add("F:\\Folder1\\Mosaic");
pathString.Add("H:\\Folder2\\Mosaic");
pathString.Add("D:\\Folder2\\Mosaic");
pathString.Add("D:\\Folder2\\Mosaic\\SubFolder");
pathString.Add("E:\\Dataset\\Mosaic");
Dictionary<String, int> PathDict = new Dictionary<string,int>();
foreach (String str in pathString)
{
int count = 0;
foreach (char c in str)
if (c == '\\') count++;
while (count > 0)
{
int index = nthOccurrence(str, '\\', count);
String tempPath;
if (index < 0)
{
//Console.WriteLine(str);
tempPath = str;
}
else
{
//Console.WriteLine(str.Substring(0,index));
tempPath = str.Substring(0, index);
}
if (PathDict.ContainsKey(tempPath))
{
PathDict[tempPath]++;
}
else
{
foreach (var keys in PathDict.Keys)
{
if (tempPath.IndexOf(keys) > 0)
{
PathDict[keys]++;
}
}
PathDict.Add(tempPath, 1);
}
count--;
}
}
foreach(var keyValue in PathDict){
if(keyValue.Value > 1)
Console.WriteLine(keyValue.Key);
/*Console.WriteLine(keyValue.Key + " - " + keyValue.Value);*/
}
}
}
}
这是一个非常简单的程序,假设您将路径放在字符串中并检查重复项。如果对于大量路径,您必须采用更有效的解决方案。