gpt4 book ai didi

powershell - “The LinkedList node does not belong to current LinkedList”

转载 作者:行者123 更新时间:2023-12-02 23:28:14 26 4
gpt4 key购买 nike

在尝试了解如何将哈希表插入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}) 

最佳答案

基本上,使用foreach,您可以遍历值(hashtable),而不是LinkedListNode,这是AddAfter方法的预期输入。我建议对列表进行如下迭代-

#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})
}

$current = $list.First

while(-not ($current -eq $null))
{
If($current.Value.X -eq 105)
{
$list.AddAfter($current, @{ID=128;X=128;Y=128})
Break
}

$current = $current.Next
}

ForEach($item In $list){
write-host "ID:"$item.ID", X:"$item.x", Y:"$item.y", TYPE:" $item.GetType()
}
#-----------------------------------
#END SAMPLE SCRIPT

关于powershell - “The LinkedList node does not belong to current LinkedList”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27139822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com