我正在使用 Delphi 9 的 TDictionary 泛型类。我的 TDictionary 看起来像这样:
g_FileHandlers : TDictionary<String, TList<String>>;
而且,所以我像这样初始化 TDictionary:
g_FileHandlers := TDictionary<String, TList<String>>.Create;
我有一个 TList,我也在初始化,这样我就可以用它来填充 TDictionary。我正在循环一些用于填充 TList/TDictionary 的文件数据,并且我试图重新使用相同的 TList 作为值插入到 TDictionary 中。在第一次插入 TDictionary 时,项目的 TList 值存在并且其中包含数据。在第二次和随后的迭代中,TList 值全部为零。
g_FilePaths := TList<String>.Create;
在我看来,这一切都是通过参考来完成的。如何按值而不是引用将 TList 添加到我的 TDictionary?
// Create our dictionary of files and handlers
for i := 0 to g_FileList.Count - 1 do
begin
g_HandlerName := AnsiMidStr(g_FileList[i], 2, Length(g_FileList[i]));
g_HandlerName := AnsiMidStr(g_HandlerName, 1, Pos('"', g_HandlerName) - 1);
if i = 0 then
g_PreviousHandlerName := g_HandlerName;
if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) = 0 then
begin
g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
g_FilePaths.Add(g_FilePath);
end
else
begin
g_FileHandlers.Add(g_PreviousHandlerName, g_FilePaths);
g_FilePaths.Clear;
g_FilePath := AnsiMidStr(g_FileList[i], Length(g_HandlerName) + 5, Length(g_FileList[i]));
g_FilePath := AnsiMidStr(g_FilePath, 1, Length(g_FilePath) - 1);
g_FilePaths.Add(g_FilePath);
end;
if AnsiCompareText(g_HandlerName, g_PreviousHandlerName) <> 0 then
g_PreviousHandlerName := g_HandlerName;
if i = g_FileList.Count - 1 then
g_FileHandlers.Add(g_HandlerName, g_FilePaths);
end;
g_FilePaths.Free;