arrays - 如何在 PowerShell 中创建数组的数组?

标签 arrays powershell

我想在 PowerShell 中创建一个数组的数组。

$x = @(
    @(1,2,3),
    @(4,5,6)
)

效果很好。然而,有时我的数组列表中只有一个数组。在这种情况下,PowerShell 会忽略以下列表之一:

$x = @(
    @(1,2,3)
)

$x[0][0] # Should return 1
Unable to index into an object of type System.Int32.
At line:1 char:7
+ $a[0][ <<<< 0]
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : CannotIndex

如何创建一个数组数组,并保证即使该数组中只有一个数组项,它仍然是一个二维数组?

最佳答案

添加逗号来创建数组:

$x = @(
    ,@(1,2,3)
)

简单的方法:

$x = ,(1,2,3)

关于arrays - 如何在 PowerShell 中创建数组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11138288/

相关文章:

javascript - 如何在 JavaScript 中声明二维数组?

php - 为什么 in_array() 使用这些(大数字)字符串错误地返回 true?

powershell - 远程Powershell弹出消息

c# - EventWaitHandle "No handle of the given name exists"Vagrant 远程虚拟机 C# PowerShell

powershell - 通过 "Command Line"参数搜索 taskkill 的批处理脚本

powershell - 新-AzureRmResourceGroup : 'this.Client.SubscriptionId' cannot be null

javascript - 无法将新数据添加到基于 switch case 的数组

c - 删除数组的元素

javascript - 拆分字符串后在 Javascript 函数中返回数组

arrays - 查找两个 PowerShell 数组中的共同项