Array
Array are fixed size
Array stores the references, not the values, so when modifying items in a nested array, it will change everything
Basic syntax
$mylist=@()
$mylist = 1,2,3,4,5
$mylist = echo 1 2 3 4 5Get/Set
$mylist[$i]
$mylist[$i..$j] # Slicing
$mylist[$i,$j,$i] # Slicing with custom
$mylist[$i] = $i- if index out of range, no error will occur, it will be
$null - PowerShell Slicing IS inclusive
Count
$mylist.Count # or LengthAdd item into an array
$mylist+="fruits"
$mylist+=@(1,2) # will be ...,1,2 not ...,[1,2]- when using this method to add a list, it will unpack the list
Remove an item from an array
$mylist=$mylist -ne 1-neget all the items in that array that does not equal to that value, hence removing that item
Creating an array from string
"1,2,3".split(";")Operators
Join the array into a string
$mylist -join "-" # the delimiter can be emptyChecking membership
$var -in $mylist
$mylist -contains $var
$mylist -ccontains $var- the
ccontainmakes the match case sensitive
Filter
$mylist | where {$_ -eq condition }ArrayList
It’s a dynamic size
$mylist = New-Object -TypeName System.Collections.ArrayListAdd
$mylist.Add(1) # return the index
$mylist.AddRange(@()) # add a range from an array, similar to extend- add will always echo into the termina, use void to
[void]$mylist.Add(1)Remove
$mylist.Remove(value)
$mylist.RemoveAt(index)
$mylist.RemoveRange(index, deleteCount)- remove is not the same as pop
- it remove one instance where it matches the value from the beginning
RemoveRangeis similar to JavaScript splice