• for in 循环


    本文转自万一老师的博客

    原文出处: http://www.cnblogs.com/del/archive/2008/11/12/1332011.html

     

    此语句只对Delphi 2005以后的版本有效

    一、遍历 Tstrings
    --------------------------------------------------------------------------------
     
    var
      List: TStrings;
      s: string;
    begin
      List := TStringList.Create;
      List.CommaText := 'aaa,bbb,ccc';

      for s in List do
        ShowMessage(s);

      List.Free;
    end;
    --------------------------------------------------------------------------------

    二、遍历数组
    --------------------------------------------------------------------------------
     
    var
      Arr: array[0..2] of Byte;
      i: Integer;
      b: Byte;
    begin
      for i := Low(Arr) to High(Arr) do
        Arr[i] := Random(MAXBYTE);

      for b in Arr do
        ShowMessage(IntToStr(b));
    end;
    --------------------------------------------------------------------------------

    三、遍历子界
    --------------------------------------------------------------------------------
     
    {
    1}
    var
      sub: 0..9;
      str: string;
    begin
      str := '';
      for sub in [Low(sub)..High(sub)] do
        str := str + IntToStr(sub); 

      ShowMessage(str); {0123456789}
    end;

    {
    2}
    type
      TSub = 'A'..'G';
    var
      sub: TSub;
      str: string;
    begin
      str := '';
      for sub in [Low(sub)..High(sub)] do
        str := str + sub; 

      ShowMessage(str); {ABCDEFG}
    end;

    {
    3}
    var
      sub: Byte; {Byte
    应该算是个 0..255 的子界}
      num: Cardinal;
    begin
      num := 0;
      for sub in [Low(sub)..High(sub)] do
        Inc(num, sub); 

      ShowMessage(IntToStr(num)); {32640}
    end;
    --------------------------------------------------------------------------------

    四、遍历枚举
    --------------------------------------------------------------------------------
     
    type
      TEnum = (Red,Green,Blue);
    var
      enum: TEnum;
      count: Integer;
    begin
      count := 0;
      for enum in [Low(enum)..High(enum)] do
        Inc(count);

      ShowMessage(IntToStr(count)); {3}
    end;
    --------------------------------------------------------------------------------

    五、遍历集合
    --------------------------------------------------------------------------------
     
    type
      TEnum = (Red,Green,Blue,Yellow);
      TSet = set of TEnum;
    var
      set1: set of TEnum;
      set2: TSet;
      elem: Tenum;
      count: Integer;
    begin
      set1 := [Red, Yellow];
      count := 0;
      for elem in set1 do Inc(count);
      ShowMessage(IntToStr(count)); {2}

      set2 := [Red..Yellow];
      count := 0;
      for elem in set2 do Inc(count);
      ShowMessage(IntToStr(count)); {4}
    end;
    --------------------------------------------------------------------------------

    六、遍历字符串
    --------------------------------------------------------------------------------
     
    var
      str: string;
      c: Char;
    begin
      str := 'ABCD';
      for c in str do 
        ShowMessage(c);
    end;
    --------------------------------------------------------------------------------

    上面是我能够想到的, 还有吗? 帮我想想
    --------------------------------------------------------------------------------

  • 相关阅读:
    2017级算法第二次上机-B.第k顺序统计量
    2017级算法第二次上机-B.女娲加农炮
    2017级算法第二次上机-A.ModricWang's Real QuickSort Query
    2017级算法第三次上机-G.ModricWang的导弹拦截系统
    2017级算法第三次上机-F. SkyLee炒股票
    2017级算法第三次上机-C.SkyLee组装电脑
    Objective-c快速入门
    [翻译] Android是怎样绘制视图的
    AnimationsDemo中的ZoomActivity代码分析
    getGlobalVisibleRect和getLocalVisibleRect
  • 原文地址:https://www.cnblogs.com/w413133157/p/1731037.html
Copyright © 2020-2023  润新知