fbpx Create a Jagged or Multidimensional Array | Windows PowerShell, Windows Server | HostingUltraso.com
24/7/365 Support

Create a Jagged or Multidimensional Array

Problem

You want to create an array of arrays, or an array of multiple dimensions.

Solution

To create a jagged multidimensional array (an array of arrays), use the @( ) array syntax:

PS >$jagged = @(

>> (1,2,3,4),

>> (5,6,7,8)

>> )

>>

PS >$jagged[0][1]

>>2

PS >$jagged[1][3]

>>8

To create a (nonjagged) multidimensional array, use the NewObject cmdlet:

PS >$multidimensional = NewObject "int32[,]" 2,4 PS >$multidimensional[0,1] = 2 PS >$multidimensional[1,3] = 8 PS > PS >$multidimensional[0,1] >>2 PS >$multidimensional[1,3] >>8

Discussion

Jagged and multidimensional arrays are useful for holding lists of lists/arrays of arrays. Jagged arrays are much easier to work with (and use less memory), while nonjagged multidimensional arrays are sometimes useful for dealing with large grids of data.

Since a jagged array is an array of arrays, creating an item in a jagged array follows the same rules as creating an item in a regular array. If any of the arrays are singleelement arrays, use the unary comma operator. For example, to create a jagged array with one nested array of one element:

PS >$oneByOneJagged = @( >> ,(,1) >> PS >$oneByOneJagged[0][0]

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say