Delphi中for、while 使用注意事项


循环查找并删除指定条件元素:

  1. program Project2;
  2. {$APPTYPE CONSOLE}
  3. uses
  4.   SysUtils, Classes;
  5. var
  6.   StrList: TStringList;
  7.   J: Integer;
  8. procedure DeleteByWhile;
  9. var
  10.   I: Integer;
  11. begin
  12.   I := 0;
  13.   while I<StrList.Count do
  14.   begin
  15.     Writeln(Format('While Loop I=%d StrList.Count=%d', [I, StrList.Count]));
  16.     if StrList[I] = '3' then
  17.     begin
  18.       StrList.Delete(I);
  19.     end;
  20.     I := I+1;
  21.   end;
  22. end;
  23. procedure DeleteByFor;
  24. var
  25.   I: Integer;
  26. begin
  27.   for I := 0 to StrList.Count - 1 do
  28.   begin
  29.     Writeln(Format('For Loop I=%d StrList.Count=%d', [I, StrList.Count]));
  30.     if StrList[I] = '3' then
  31.     begin
  32.       StrList.Delete(I);
  33.     end;
  34.   end;
  35. end;
  36. begin
  37.   try
  38.     StrList := TStringList.Create;
  39.     try
  40.       for J := 0 to 10 - 1 do
  41.         StrList.Add(IntToStr(J));
  42.       //DeleteByWhile;
  43.       DeleteByFor;
  44.     finally
  45.       StrList.Free;
  46.     end;
  47.   except
  48.     on E:Exception do
  49.       Writeln(E.Classname, ': ', E.Message);
  50.   end;
  51.   Sleep(300000);
  52. end.

调用DeleterByFor运行结果:
For Loop I=0 StrList.Count=10
For Loop I=1 StrList.Count=10
For Loop I=2 StrList.Count=10
For Loop I=3 StrList.Count=10
For Loop I=4 StrList.Count=9
For Loop I=5 StrList.Count=9
For Loop I=6 StrList.Count=9
For Loop I=7 StrList.Count=9
For Loop I=8 StrList.Count=9
For Loop I=9 StrList.Count=9
EStringListError: List index out of bounds (9)

调用DeleteByWhile运行结果:
While Loop I=0 StrList.Count=10
While Loop I=1 StrList.Count=10
While Loop I=2 StrList.Count=10
While Loop I=3 StrList.Count=10
While Loop I=4 StrList.Count=9
While Loop I=5 StrList.Count=9
While Loop I=6 StrList.Count=9
While Loop I=7 StrList.Count=9
While Loop I=8 StrList.Count=9

从上面的实例可以看出,for循环中循环最小和最大值是在一开始就确定了,在程序运行过程中不会因为其他因素的影响而改变;而while循环是每次都进行条件比较,而且是动态的读取参数的值。

 

如果将DeleteByFor函数改成:

  1. procedure DeleteByFor;
  2. var
  3.   I: Integer;
  4. begin
  5.   for I := StrList.Count - 1 downto 0 do
  6.   begin
  7.     Writeln(Format('For Loop I=%d StrList.Count=%d', [I, StrList.Count]));
  8.     if StrList[I] = '3' then
  9.     begin
  10.       StrList.Delete(I);
  11.     end;
  12.   end;
  13. end;

则运行结果:
For Loop I=9 StrList.Count=10
For Loop I=8 StrList.Count=10
For Loop I=7 StrList.Count=10
For Loop I=6 StrList.Count=10
For Loop I=5 StrList.Count=10
For Loop I=4 StrList.Count=10
For Loop I=3 StrList.Count=10
For Loop I=2 StrList.Count=9
For Loop I=1 StrList.Count=9
For Loop I=0 StrList.Count=9

从上面的结果可以总结出,当我们要循环查找并且删除指定条件的元素时,一定要用while或者由大到小的for循环。否则会引起不必要的错误!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值