0

当我尝试为数组的特定索引分配一个值时,它显示了错误。 PS /home/mifi> $names = @()

PS /home/mifi> $names[1]="abc" Index was outside the bounds of the array. At line:1 char:1 + $names[1]="abc" + ~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException + FullyQualifiedErrorId : System.IndexOutOfRangeException

pwsh -v
PowerShell v6.0.2

`

4

1 回答 1

1

那是因为您必须先初始化数组。如果你知道数组的大小,你可以这样做:

$arraySize = 10
$names= 1..$arraySize | foreach { $null }

考虑使用哈希表

$names = @{}
$names[1] = "abc"
于 2018-06-07T06:36:18.093 回答