1 var 2 List: TList<Integer>; 3 FoundIndex: Integer; 4 5 begin 6 { Create a new List. } 7 List := TList<Integer>.Create; 8 { Add a few values to the list. } 9 List.AddRange([5, 1, 8, 2, 9, 14, 4, 5, 1]); 10 11 writeln('Index of first 1 is ' + IntToStr(List.IndexOf(1))); 12 writeln('Index of last 1 is ' + IntToStr(List.LastIndexOf(1))); 13 writeln('Does List contains element 100? ' + BoolToStr(List.Contains(100))); 14 15 { Add another element to the list. } 16 List.Add(100); 17 18 writeln('There are ' + IntToStr(List.Count) + ' elements in the list.'); 19 20 { Remove the first occurrence of 1. } 21 List.Remove(1); 22 { Delete a few elements from position 0. } 23 List.Delete(0); 24 List.DeleteRange(0, 2); 25 { Extract the remaining 1 from the list. } 26 List.Extract(1); 27 { Set the capacity to the actual length. } 28 List.TrimExcess; 29 writeln('Capacity of the list is ' + IntToStr(List.Capacity)); 30 31 { Clear the list. } 32 List.Clear; 33 { Insert some elements. } 34 List.Insert(0, 2); 35 List.Insert(1, 1); 36 List.InsertRange(0, [6, 3, 8, 10, 11]); 37 38 { Sort the list. } 39 List.Sort; 40 41 { Binary search for the required element. } 42 if List.BinarySearch(6, FoundIndex) then //必须是Sorted 数列,并且是从小到大排列,序号从1开始 43 writeln('Found element 6 at index ' + IntToStr(FoundIndex)); 44 45 { Reverse the list. } 46 List.Reverse; 47 writeln('The element on position 0 is ' + IntToStr(List.Items[0])); 48 List.Free; 49 readln; 50 51 end.
分类:
Delphi技巧
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本