Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 获取数组的值而不是索引_Arrays_Powershell_Hashmap - Fatal编程技术网

Arrays 获取数组的值而不是索引

Arrays 获取数组的值而不是索引,arrays,powershell,hashmap,Arrays,Powershell,Hashmap,我试图检索一个数组,最后我想把它们作为一个列表放到一个散列中,但我只得到索引,而不是值列表 $testArray = [System.Collections.ArrayList]@() $tempArray = "123", "321", "453" foreach($item in $tempArray) { if ($item -notlike 123) { $arrayID = $testArray.Add($item); } } @{"mailboxes

我试图检索一个数组,最后我想把它们作为一个列表放到一个散列中,但我只得到索引,而不是值列表

$testArray = [System.Collections.ArrayList]@()
$tempArray = "123", "321", "453"
foreach($item in $tempArray) {
    if ($item -notlike 123) {
        $arrayID = $testArray.Add($item);
    }
}

@{"mailboxes" = $arrayID};
因此,我希望看到如下值

mailboxes {321, 453} 邮箱{321453} 而不是像下面这样的索引

mailboxes 1 邮箱1
ArrayList.Add()
方法返回新数组元素的索引,因此您只需向
$arrayID
添加一个整数。相反:

$testArray = [System.Collections.ArrayList]@()
$tempArray = "123", "321", "453"
foreach($item in $tempArray) {
    if ($item -notlike 123) {
        $testArray.Add($item);
    }
}
@{ "mailboxes" = $testArray};
返回:

mailboxes                      {321, 453}
ArrayList.Add()
方法返回新数组元素的索引,因此您只需向
$arrayID
添加一个整数。相反:

$testArray = [System.Collections.ArrayList]@()
$tempArray = "123", "321", "453"
foreach($item in $tempArray) {
    if ($item -notlike 123) {
        $testArray.Add($item);
    }
}
@{ "mailboxes" = $testArray};
返回:

mailboxes                      {321, 453}

如果你的最终目标是得到这个

mailboxes {321, 453}
那你就可以这么做了

$OldArray=@(“123”、“321”、“453”)
[System.Collections.ArrayList]$NewArray=@()
foreach($OldArray中的项){
如果($项目-ne 123){
$NewArray+=项目
}
}
@{“邮箱”=$NewArray};
或者只是这个

{“邮箱”=(“123”、“321”、“453”)|?{$-ne 123};
旁注: 默认情况下,
@()
创建大小固定的
[system.array]
。在
[system.array]
上使用
+=
将导致创建一个新数组,并且需要将旧数组内容复制到其中。
[System.Collections.ArrayList]
是动态调整大小的,允许您轻松添加新项目。

如果您的最终目标是实现这一点

mailboxes {321, 453}
那你就可以这么做了

$OldArray=@(“123”、“321”、“453”)
[System.Collections.ArrayList]$NewArray=@()
foreach($OldArray中的项){
如果($项目-ne 123){
$NewArray+=项目
}
}
@{“邮箱”=$NewArray};
或者只是这个

{“邮箱”=(“123”、“321”、“453”)|?{$-ne 123};
旁注: 默认情况下,
@()
创建大小固定的
[system.array]
。在
[system.array]
上使用
+=
将导致创建一个新数组,并且需要将旧数组内容复制到其中。
[System.Collections.ArrayList]
具有动态大小,允许您轻松添加新项目。

{“邮箱”=$arrayID}
->
{“邮箱”=$testArray}
{“邮箱”=$arrayID}->
{“邮箱”=$testArray}
感谢您指出这个问题,但我还是得到了如下输出:0 1名称值-----邮箱{321453}如何避免索引部分,即01@rora设置$null=$testArray.Add($item)或只是[void]$testArray.Add($item)要阻止索引被返回,这确实对我有帮助。感谢您指出问题所在,但我仍然得到如下输出:0 1名称值-----邮箱{321453}如何避免索引部分,即01@rora设置$null=$testArray.Add($item)或只是[void]$testArray.Add($item)来停止返回索引这对我有帮助。