【发布时间】:2015-01-24 05:31:40
【问题描述】:
我在尝试了解如何将哈希表插入 LinkedLists 时遇到了困难。我忘记了我尝试过的不同的东西。我知道我可以使用 ArrayList 或其他东西,但我想用 LinkedLists 来完成这项工作,以便我可以对其进行基准测试......
这是我想出的:
#BEGIN SAMPLE SCRIPT
#------------------------
$list = New-Object Collections.Generic.LinkedList[Hashtable]
For($i=1; $i -lt 10; $i++){
$list.AddLast(@{ID=$i; X=100+$i;Y=100+$i})
}
ForEach($item In $list){
If($Item.x -eq 105){
$list.AddAfter($item, @{ID=128;X=128;Y=128})
Break
}
}
ForEach($item In $list){
write-host "ID:"$item.ID", X:"$item.x", Y:"$item.y", TYPE:" $item.GetType()
}
#-----------------------------------
#END SAMPLE SCRIPT
预期输出:
ID: 1 , X: 101 , Y: 101 , TYPE: System.Collections.Hashtable
ID: 2 , X: 102 , Y: 102 , TYPE: System.Collections.Hashtable
ID: 3 , X: 103 , Y: 103 , TYPE: System.Collections.Hashtable
ID: 4 , X: 104 , Y: 104 , TYPE: System.Collections.Hashtable
ID: 5 , X: 105 , Y: 105 , TYPE: System.Collections.Hashtable
ID: 128 , X: 128 , Y: 128 , TYPE: System.Collections.Hashtable
ID: 6 , X: 106 , Y: 106 , TYPE: System.Collections.Hashtable
ID: 7 , X: 107 , Y: 107 , TYPE: System.Collections.Hashtable
ID: 8 , X: 108 , Y: 108 , TYPE: System.Collections.Hashtable
ID: 9 , X: 109 , Y: 109 , TYPE: System.Collections.Hashtable
我得到的错误:
Exception calling "AddAfter" with "2" argument(s):
"The LinkedList node does not belong to current LinkedList."
触发错误信息的行:
$list.AddAfter($item, @{ID=128;X=128;Y=128})
【问题讨论】:
标签: powershell collections linked-list hashtable