Multi-dimensional and jagged arrays

Given that an array contains objects, an array can therefore also contain other arrays.

For example, an array that contains other arrays (a multi-dimensional array) might be created as follows:

$arrayOfArrays = @( 
    @(1, 2, 3), 
    @(4, 5, 6), 
    @(7, 8, 9) 
) 

Be careful to ensure that the comma following each of the inner arrays (except the last) is in place. If that comma is missing, the entire structure will be flattened, merging the three inner arrays.

Elements in the array are accessed by indexing into each array in turn (starting with the outermost). The element with the value 2 is accessible using the following notation:

PS> $arrayOfArrays[0][1]
2

This states that we wish to retrieve the first element (which is an array) and the second element of that array.

The element with the value 6 is accessible using the following:

PS> $arrayOfArrays[1][2]
6

Jagged arrays are a specific form of multi-dimensional array. An example of a jagged array is as follows:

$arrayOfArrays = @( 
    @(1,  2), 
    @(4,  5,  6,  7,  8,  9), 
    @(10, 11, 12) 
) 

As in the first example, it is an array containing arrays. Instead of containing inner arrays, which all share the same size (dimension), the inner arrays have no consistent size (hence, they are jagged).

In this example, the element with the value 9 is accessed as follows:

PS> $arrayOfArrays[1][5]
9
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.100.120