如何在 PowerShell 中创建数组数组? [英] How do I create array of arrays in PowerShell?

查看:29
本文介绍了如何在 PowerShell 中创建数组数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 PowerShell 中创建一个数组数组.

I want to create an array of arrays in PowerShell.

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

它工作正常.但是,有时我的数组列表中只有一个数组.在这种情况下,PowerShell 会忽略其中一个列表:

It works fine. However, sometimes I have only one array in the array list. In that situation, PowerShell ignores one of the lists:

$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

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

How do I create an array of arrays, guaranteed it remains as a two-dimensional array even if the array has only one array item in it?

推荐答案

添加逗号强制创建数组:

Adding a comma force to create an array:

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

简单的方法:

$x = ,(1,2,3)

这篇关于如何在 PowerShell 中创建数组数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆