本帖最后由 wrove 于 2012-11-2 08:56 编辑
- @Echo off&SetLocal EnableDelayedExpansion&Color a
- Echo 0.创建一个数组$days,给它赋予周一到周五的工作日名称
- Call :@Array "$days" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 1.在数组$days开始位置压入一个元素"Sunday"
- Call :%$days.Push% "Sunday"
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 2.插入"Hello World"到数组$days的第3索引位置
- Call :%$days.Insert% 3 "Hello World"
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 3.删除数组$days的第3索引位置上的元素
- Call :%$days.Remove% 3
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 4.在数组$days的末尾位置追加一个元素"Saturday"
- Call :%$days.Append% "Saturday"
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 5.在数组$days的末尾追加另一个数组$Signs
- Echo.
- ECho 5.1创建数组$Signs,给它添加两个元素"+"、"-"
- Echo.
- Call :@Array "$Signs" "+" "-"
- Echo 5.2回显数组$Signs的信息
- Echo.
- Call :%$Signs.ToString%
- Echo.
- Echo 5.3将$Signs的所有元素追加到$days末尾
- Call :%$days.Extend% "$Signs"
- Echo.
- Echo 5.4回显追加$Signs数组后$days数组的信息
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 6.反转$days数组的元素
- Call :%$days.Reserve%
- Echo.
- Call :%$days.ToString%
- Echo.
- Echo 9.删除数组$days的数据与方法
- Call :%$days.Delete%
- Echo =================All Done==================
- Echo Press any key to quit...
- Pause>nul
- Exit
-
-
- Rem =================@Array====================
- :@Array
- Rem 初始化临时变量
- Set __this=%~1
- Set __index=-1
- :Array_Begin
- Rem 第一次调用建立类型固有信息
- If not Defined $Array.Methods (
- Set $Array.Fields="Type UBound [i]"
- Set $Array.Methods="Push Insert Remove Append Extend Reserve ToString Delete"
- )
- Rem *******************************************
- Rem 初始化对象字段
- Rem 设置对象的类型信息
- Set %__this%.Type="@Array"
- Rem 设置数组元素
- For %%a In (%*) Do (
- Set %__this%[!__index!]=%%a
- Set /a __index+=1
- )
- Rem 删除无用的数组元素,即位于-1索引位置的元素
- Set %__this%[-1]=
- Rem 设置数组的最大下标
- Set /a %__this%.UBound=%__index%-1
- Rem *******************************************
- Rem 初始化对象方法
- For %%a In (%$Array.Methods:"=%) Do (
- Set %__this%.%%a=Array.%%a %__this%
- )
- Rem *******************************************
- :Array_End
- Rem 销毁临时变量
- Set __this=
- Set __index=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Push
- Rem 初始化临时变量
- Set __this=%~1
- Set /a __index=!%__this%.UBound!+1
- :Array.Push_Begin
- Rem *******************************************
- For /L %%a In (!%__this%.UBound!,-1,0) Do (
- Set %__this%[!__index!]=!%__this%[%%a]!
- Set /a __index-=1
- )
- Set /a %__this%.UBound+=1
- Set %__this%[0]=%2
- Rem *******************************************
- :Array.Push_End
- Rem 销毁临时变量
- Set __this=
- Set __index=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Insert
- Rem 初始化临时变量
- Set __this=%~1
- Set /a __index=!%__this%.UBound!+1
- :Array.Insert_Begin
- Rem *******************************************
- For /L %%a In (!%__this%.UBound!,-1,%~2) Do (
- Set %__this%[!__index!]=!%__this%[%%a]!
- Set /a __index-=1
- )
- Set %__this%[%~2]=%3
- Set /a %__this%.UBound+=1
- Rem *******************************************
- :Array.Insert_End
- Rem 销毁临时变量
- Set __this=
- Set __index=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Remove
- Rem 初始化临时变量
- Set __this=%~1
- Set __index=%~2
- Set /a __Start=%~2+1
- :Array.Remove_Begin
- Rem *******************************************
- For /L %%a In (%__Start%,1,!%__this%.UBound!) Do (
- Set %__this%[!__index!]=!%__this%[%%a]!
- Set /a __index+=1
- )
- Set /a %__this%.UBound-=1
- Rem *******************************************
- :Array.Remove_End
- Rem 销毁临时变量
- Set __this=
- Set __index=
- Set __Start=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Append
- Rem 初始化临时变量
- Set __this=%~1
- :Array.Append_Begin
- Rem *******************************************
- Set /a %__this%.UBound+=1
- Set %__this%[!%__this%.UBound!]=%2
- Rem *******************************************
- :Array.Append_End
- Rem 销毁临时变量
- Set __this=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Extend
- Rem 初始化临时变量
- Set __this=%~1
- Set __OtherObj=%~2
- If not !%__OtherObj%.Type!=="@Array" (
- Echo Error:要求参数%__OtherObj%是一个数组
- )
- Set /a __index=!%__this%.UBound!+1
- :Array.Extend_Begin
- Rem *******************************************
- For /L %%a In (0,1,!%__OtherObj%.UBound!) Do (
- Set %__this%[!__index!]=!%__OtherObj%[%%a]!
- Set /a __index+=1
- )
- Set /a %__this%.UBound+=!%__OtherObj%.UBound!+1
- Rem *******************************************
- :Array.Extend_End
- Rem 销毁临时变量
- Set __this=
- Set __OtherObj=
- Set __index=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Reserve
- Rem 初始化临时变量
- Set __this=%~1
- Set /a __LEnd=!%__this%.UBound!/2
- Set /a __Mod=!%__this%.UBound!%2
- If %__Mod% equ 0 (
- Set /a __LEnd=%__LEnd%-1
- )
- Set __RIndex=!%__this%.UBound!
- :Array.Reserve_Begin
- Rem *******************************************
- For /L %%a In (0,1,%__LEnd%) Do (
- Set __Temp=!%__this%[%%a]!
- Call Set %__this%[%%a]=%%%__this%[!__RIndex!]%%
- Set %__this%[!__RIndex!]=!__Temp!
- Set /a __RIndex-=1
- )
- Rem *******************************************
- :Array.Reserve_End
- Rem 销毁临时变量
- Set __this=
- Set __LEnd=
- Set __Mod=
- Set __RIndex=
- Set __Temp=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.ToString
- Rem 初始化临时变量
- Set __this=%~1
- Set __Str=[
- :Array.ToString_Begin
- Rem *******************************************
- For /L %%a In (0,1,!%__this%.UBound!) Do Set __Str=!__Str!!%__this%[%%a]!,
- Set __Str=%__Str:~,-1%]
- Echo %__Str%
- Rem *******************************************
- Rem 销毁临时变量
- :Array.ToString_End
- Set __this=
- Set __Str=
- Goto :Eof
- Rem -------------------------------------------
-
- Rem -------------------------------------------
- :Array.Delete
- Rem 初始化临时变量
- Set __this=%~1
- :Array.Delete_Begin
- Rem *******************************************
- Rem 删除对象的数据
- Rem 删除对象类型信息
- Set %__this%.Type=
- Rem 删除数组元素
- For /L %%a In (0,1,!%__this%.UBound!) Do (
- Set %__this%[%%a]=
- )
- Set %__this%=
- Rem 删除对象的方法
- For %%a In (%$Array.Methods:"=%) Do (
- Set %__this%.%%a=
- )
- Rem *******************************************
- :Array.Delete_End
- Rem 销毁临时变量
- Set __this=
- Goto :Eof
- Rem =================@Array====================
复制代码
|