我需要将 HashSet 转换为 ArrayList 吗?
$hashset = New-Object System.Collections.Generic.HashSet[int]
$hashset.Add(1)
$hashset.Add(2)
$hashset.Add(3)
$arraylist = New-Object System.Collections.ArrayList
# Now what?
最佳答案
一种方法,使用 CopyTo
:
$array = New-Object int[] $hashset.Count
$hashset.CopyTo($array)
$arraylist = [System.Collections.ArrayList]$array
另一种方式(更短,但对于大型哈希集更慢):
$arraylist = [System.Collections.ArrayList]@($hashset)
还有,我强烈推荐收藏
List
在 ArrayList
, 因为它几乎是 deprecated自从引入泛型以来:$list = [System.Collections.Generic.List[int]]$hashset
关于arrays - 如何在 PowerShell 中将 HashSet 转换为 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962058/