如何在PowerShell中找到ArrayList中的indexOf元素

时间:2016-01-28 15:38:08

标签: powershell powershell-v2.0 powershell-v3.0

我试图使用powershell脚本从arraylist中找到元素的索引,但是得到以下给定的错误

  

方法调用失败,因为[System.String []]不包含   方法名为'IndexOf'。

使用的代码:

[String[]]$eventList = New-Object System.Collections.ArrayList
$eventList.GetType().FullName 
$index = $eventList.IndexOf('cvv');

1 个答案:

答案 0 :(得分:1)

您正在将ArrayList强制转换为数组,只需删除强制转换:

$eventList = New-Object System.Collections.ArrayList

此外,您可能需要考虑使用List< string>而不是ArrayList:

$eventList = New-Object System.Collections.Generic.List[string]