(view source code of arraylist.vbs as plain text)
' ArrayLst.vbs, Version 1.00' Demonstrates the use of the .NET Frameworks ArrayList class in VBScript'' Based on the "Hey, Scripting Guy!" article "Be Careful What You Say"' http://www.microsoft.com/technet/technetmag/issues/2007/01/HeyScriptingGuy/default.aspx'' Written by Rob van der Woude' http://www.robvanderwoude.comOption ExplicitDim DataList, SecondList' create the first ArrayListSet DataList = CreateObject( "System.Collections.ArrayList" )
' adding elements is really a snapDataList.Add "F"
DataList.Add "B"
DataList.Add "D"
DataList.Add "C"
' show the content and size of the ArrayList' Note: ShowList is a custom subroutine,' not a "native" VBScript statementShowList DataList, "Initial 4 elements"' add some more elementsDataList.Add "E"
DataList.Add "D"
DataList.Add "A"
DataList.Add "D"
' (show the result)ShowList DataList, "Append another 4"' removing an element by its name isn't difficult eitherDataList.Remove "D"
' (show the result)ShowList DataList, "Remove D"' sorting doesn't require multiple lines of codeDataList.Sort
' (show the result)ShowList DataList, "Sort"' neither does reversing the order of the elementsDataList.Reverse
' (show the result)ShowList DataList, "Reverse"' remove another element by its nameDataList.Remove "D"
' (show the result)ShowList DataList, "Removed D"' remove another elementDataList.Remove "D"
' (show the result)ShowList DataList, "Remove D"' create a second ArrayListSet SecondList = CreateObject( "System.Collections.ArrayList" )
' populate it with some dataSecondList.Add "3"
SecondList.Add "8"
SecondList.Add "2"
SecondList.Add "7"
SecondList.Add "5"
SecondList.Add "1"
SecondList.Add "5"
' (show the result)ShowList SecondList, "Initial"' add the data from the second ArrayList to the first oneDataList.AddRange( SecondList )
' (show the result)ShowList DataList, "AddRange"' sort the (first) ArrayListDataList.Sort
' (show the result)ShowList DataList, "Sort"' remove some elementsDataList.Remove "3"
DataList.Remove "B"
' (show the result)ShowList DataList, "Remove 3, B"' reduce the capacity of the ArrayList to the minimum requiredDataList.TrimToSize
' (show the result)ShowList DataList, "TrimToSize"' remove an element by its indexDataList.RemoveAt 0
' (show the result)ShowList DataList, "RemoveAt 0"' remove another element by its indexDataList.RemoveAt 3
' (show the result)ShowList DataList, "RemoveAt 3"' display the first index of a named elementWScript.Echo "IndexOf( ""A"", 0 ) " & DataList.IndexOf( "A", 0 )
' remove a range of indexes from the ArrayListDataList.RemoveRange 3, 2
' (show the result)ShowList DataList, "RemoveRange 3, 2"' display the first index of an element with value "5"WScript.Echo "IndexOf( ""5"", 0 ) " & DataList.IndexOf( "5", 0 )
' display the first index after index 1 of an element with value "2"WScript.Echo "IndexOf( ""2"", 1 ) " & DataList.IndexOf( "2", 1 )
' display the last index of an element with value "5"WScript.Echo "LastIndexOf( ""5"" ) " & DataList.LastIndexOf( "5" )
WScript.Echo
' delete all elements from the second ArrayListSecondList.Clear
' add new elementsSecondList.Add "I"
SecondList.Add "II"
SecondList.Add "III"
SecondList.Add "IV"
SecondList.Add "V"
' reduce the capacity to the minimum requiredSecondList.TrimToSize
' (show the result)ShowList SecondList, "Initial"' insert an element with value "X" at index 2SecondList.Insert 2, "X"
' (show the result)ShowList SecondList, "Insert 2, ""X"""
' add the data from the second ArrayList to the first oneDataList.AddRange( SecondList )
' (show the result)ShowList DataList, "AddRange"' demonstrate some other functionality available for ArrayListsWScript.Echo "DataList.Contains( ""E"" ) = " & DataList.Contains( "E" )
WScript.Echo "DataList.IndexOf( ""E"", 0 ) = " & DataList.IndexOf( "E", 0 )
WScript.Echo "DataList.Contains( ""M"" ) = " & DataList.Contains( "M" )
WScript.Echo "DataList.IndexOf( ""M"", 0 ) = " & DataList.IndexOf( "M", 0 )
WScript.Echo "DataList( 4 ) = " & DataList( 4 )
WScript.Echo "DataList.Item( 4 ) = " & DataList.Item( 4 )
Sub ShowList( ByRef arrListName, ByVal strPrefix )
' Custom subroutine displaying the contents of an ArrayList on a' single line, and the capacity and size on a separate second line.' Arguments used are:' arrListName: the name of the ArrayList to be displayed' strPrefix: a short description to be displayed before the' actual content of the ArrayList Dim intLen, strItem, strList ' specify the maximum length of the descriptive textintLen = 20
' save the ArrayList content to a stringFor Each strItem in arrListName
strList = strList & " " & strItem
Next ' trim or pad the description to its maximum length, append the ArrayList content string, and display itWScript.Echo Left( strPrefix & Space( intLen ), intLen ) & ": " & strList
' display the ArrayList's current size and capacityWScript.Echo Left( "Count/Capacity" & Space( intLen ), intLen ) & ": " & arrListName.Count & "/" & arrListName.Capacity
WScript.Echo
End Sub
page last modified: 2025-10-11; loaded in 0.0092 seconds