arrays - 如何在 PowerShell 中将 HashSet 转换为 ArrayList?

标签 arrays list powershell arraylist

我需要将 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)

还有,我强烈推荐收藏 ListArrayList , 因为它几乎是 deprecated自从引入泛型以来:
$list = [System.Collections.Generic.List[int]]$hashset

关于arrays - 如何在 PowerShell 中将 HashSet 转换为 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52962058/

相关文章:

Javascript删除和重新排列数组元素

javascript - 如何将 uint8 数组转换为 base64 编码的字符串?

python - 复制 Pandas DF N 次

powershell - 使用驱动器号通配符启动进程

sql-server - 如何在Powershell中避免使用SMO对象的系统数据库?

c++ - 不断收到 C2664 错误 - 无法将参数从 char[10] 转换为 char

python - 结构子类列表在转换为 numpy 数组时返回错误值

c - Linux内核链表和 'typeof'错误

python - 如何只打印按字母顺序嵌套在列表、字典中的内部字典的键?

arrays - 在 Powershell 中将 JSON 数组转换为单个 JSON 对象