最简单的是一组循环。为 Delphi 使用 DeHL 或 Spring 框架可能会稍微缩短您的代码,但不会太多。
这个关于使用条件查询通用 TList 实例的问题给出了使用 Spring 框架中的集合的提示。
我的示例使用Integer
而不是您的接口类型,但它应该很容易适应。
以下是两种拆分方法,具体取决于您是希望每个列表从变化的值开始,还是仅针对不同的值(如大卫所说:万岁TDictionary<Key, Value>
)
我看到了区别,我把你的例子翻了一番:
1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 1, 1, 2, 2, 2, 2, 3, 4, 4, 4
第一个算法将返回这些:
1, 1
2, 2, 2, 2
3
4, 4, 4
1, 1
2, 2, 2, 2
3
4, 4, 4
第二个:
1, 1, 1, 1
2, 2, 2, 2, 2, 2, 2, 2
3, 3
4, 4, 4, 4, 4, 4
这是示例程序;只有打印需要嵌套循环。
program SplitListOfIntegersIntoSublists;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections;
type
TIntegerList = TList<Integer>;
TIntegerListList = TList<TIntegerList>;
TMain = class(TObject)
private
class function AddNewIntegerList(IntegerListList: TIntegerListList): TIntegerList;
class procedure AddValues(AllIntegers: TIntegerList);
class procedure Fill_NewListForEachValueChange(const AllIntegers: TIntegerList; const IntegerListList: TIntegerListList);
class procedure Fill_NewListForDistinctValues(const AllIntegers: TIntegerList; const IntegerListList: TIntegerListList);
class procedure Free(const IntegerListList: TIntegerListList);
class procedure Print(const IntegerList: TIntegerList); overload;
class procedure Print(const IntegerListList: TIntegerListList); overload;
public
class procedure Run;
end;
class function TMain.AddNewIntegerList(IntegerListList: TIntegerListList): TIntegerList;
begin
Result := TIntegerList.Create;
IntegerListList.Add(Result);
end;
class procedure TMain.AddValues(AllIntegers: TIntegerList);
begin
// 1, 1, 2, 2, 2, 2, 3, 4, 4, 4
AllIntegers.Add(1);
AllIntegers.Add(1);
AllIntegers.Add(2);
AllIntegers.Add(2);
AllIntegers.Add(2);
AllIntegers.Add(2);
AllIntegers.Add(3);
AllIntegers.Add(4);
AllIntegers.Add(4);
AllIntegers.Add(4);
end;
class procedure TMain.Fill_NewListForEachValueChange(const AllIntegers: TIntegerList; const IntegerListList: TIntegerListList);
var
IntegerList: TIntegerList;
Value: Integer;
begin
for Value in AllIntegers do
begin
if (IntegerListList.Count = 0) or (Value <> IntegerList.First) then
IntegerList := AddNewIntegerList(IntegerListList);
IntegerList.Add(Value);
end;
end;
class procedure TMain.Fill_NewListForDistinctValues(const AllIntegers: TIntegerList; const IntegerListList:
TIntegerListList);
type
TIntegerListDictionary = TDictionary<Integer, TIntegerList>;
var
IntegerListDictionary: TIntegerListDictionary;
IntegerList: TIntegerList;
Value: Integer;
begin
IntegerListDictionary := TIntegerListDictionary.Create();
for Value in AllIntegers do
begin
if IntegerListDictionary.ContainsKey(Value) then
IntegerList := IntegerListDictionary[Value]
else
begin
IntegerList := AddNewIntegerList(IntegerListList);
IntegerListDictionary.Add(Value, IntegerList);
end;
IntegerList.Add(Value);
end;
end;
class procedure TMain.Free(const IntegerListList: TIntegerListList);
var
IntegerList: TIntegerList;
begin
for IntegerList in IntegerListList do
IntegerList.Free;
IntegerListList.Free;
end;
class procedure TMain.Print(const IntegerList: TIntegerList);
var
Value: Integer;
First: Boolean;
begin
First := True;
for Value in IntegerList do
begin
if not First then
Write(', ');
Write(Value);
First := False;
end;
Writeln;
end;
class procedure TMain.Print(const IntegerListList: TIntegerListList);
var
IntegerList: TIntegerList;
begin
for IntegerList in IntegerListList do
Print(IntegerList);
Writeln;
end;
class procedure TMain.Run;
var
AllIntegers: TIntegerList;
IntegerListList: TIntegerListList;
begin
AllIntegers := TIntegerList.Create();
try
AddValues(AllIntegers);
Print(AllIntegers);
IntegerListList := TIntegerListList.Create();
try
Fill_NewListForEachValueChange(AllIntegers, IntegerListList);
Print(IntegerListList);
finally
Free(IntegerListList);
end;
AddValues(AllIntegers);
Print(AllIntegers);
IntegerListList := TIntegerListList.Create();
try
Fill_NewListForEachValueChange(AllIntegers, IntegerListList);
Print(IntegerListList);
finally
Free(IntegerListList);
end;
Print(AllIntegers);
IntegerListList := TIntegerListList.Create();
try
Fill_NewListForDistinctValues(AllIntegers, IntegerListList);
Print(IntegerListList);
finally
Free(IntegerListList);
end;
finally
AllIntegers.Free;
end;
end;
begin
try
TMain.Run();
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.