是否可以比较System.Collection.Arraylist中的元素,以查看其中是否有相同的元素?

是否可以比较System.Collection.Arraylist中的元素,以查看其中是否有相同的元素?,arraylist,vbscript,iteration,Arraylist,Vbscript,Iteration,基本上就是问题的标题所问的问题。如果我提示用户他们的名字,然后要求用户输入任意5个数字,最后将响应存储到arraylist中 Wscript.StdOut.WriteLine "What is your name" name = Wscript.StdIn.ReadLine Wscript.StdOut.WriteLine "Enter any number" att1 = CInt(WScript.StdIn.ReadLine) Wscript.StdOut.WriteLine "Enter

基本上就是问题的标题所问的问题。如果我提示用户他们的名字,然后要求用户输入任意5个数字,最后将响应存储到arraylist中

Wscript.StdOut.WriteLine "What is your name"
name = Wscript.StdIn.ReadLine
Wscript.StdOut.WriteLine "Enter any number"
att1 = CInt(WScript.StdIn.ReadLine)
Wscript.StdOut.WriteLine "Enter any number"
att2 = Cint(Wscript.StdIn.ReadLine)
Wscript.StdOut.WriteLine "Enter any number: "
att3 = CInt(Wscript.StdIn.ReadLine)
Wscript.StdOut.WriteLine "Enter any number: "
att4 = CInt(Wscript.StdIn.ReadLine)
Wscript.StdOut.WriteLine "Enter any number: "
att5 = CInt(Wscript.StdIn.ReadLine)

set myNumArray =  CreateObject("System.Collections.Arraylist")

myNumArray.Add att1
myNumArray.Add att2
myNumArray.Add att3
myNumArray.Add att4
myNumArray.Add att5
如何发现用户返回的任何数字是否相同

例如,用户输入5个数字:1、2、2、4、5。如何判断用户是否输入了2次?

使用字典

Set Arg = WScript.Arguments
set WshShell = createObject("Wscript.Shell")
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout

    Set Dict = CreateObject("Scripting.Dictionary")
    Do Until Inp.AtEndOfStream
        On Error Resume Next
        Line=Inp.readline
        Dict.Add Line, ""
        If Err.Number <> 0 then
            If LCase(Arg(1)) = "l" then
                Dict.Remove Line
                Dict.Add Line, ""
            End If
        End If
    Loop
    For Each thing in Dict.Keys()
        Outp.writeline thing
    Next
删除文件中保留第一次或最后一次出现的重复行

f - keeps the first occuring duplicate line
c - keeps the last occuring duplicate line
示例

filter dedup f < "%systemroot%\win.ini"
过滤重复数据消除f<%systemroot%\win.ini”
过滤器仅读取和写入标准输入和标准输出。这些选项仅在命令提示符下可用

filter <inputfile >outputfile
filter <inputfile | other_command
other_command | filter >outputfile
other_command | filter | other_command
过滤器输出文件
过滤器输出文件
其他|u命令|过滤器|其他|u命令
使用字典(但是正确地使用字典):

输出:

cscript 34890229.vbs
found 2 2 times
-使用ArrayList查看其当前内容
。包含
方法:

Option Explicit

Dim alInp : Set alInp = CreateObject("System.Collections.ArrayList")
Dim elm
For Each elm In Array(1, 2, 2, 4, 5)
    If alInp.Contains(elm) Then
       WScript.Echo "not again:", elm
    Else
       alInp.Add elm
    End If
Next
WScript.Echo Join(alInp.ToArray())
输出:

cscript 34890229-2.vbs
not again: 2
1 2 4 5
Option Explicit

Dim alInp : Set alInp = CreateObject("System.Collections.ArrayList")
Dim elm
For Each elm In Array(1, 2, 2, 4, 5)
    If alInp.Contains(elm) Then
       WScript.Echo "not again:", elm
    Else
       alInp.Add elm
    End If
Next
WScript.Echo Join(alInp.ToArray())
cscript 34890229-2.vbs
not again: 2
1 2 4 5