Turbo-Charging Arrays

by Feb 17, 2009

Simple arrays have no built-in mechanism to insert new elements or extract elements at given positions. For example, to extract the 5. element from an array, in PowerShell you'd have to work around it like this:

$array = 1..10
$array = $array[0..3] + $array[5..9]
$array

This is inconvienent and slow. A much better way is to "upgrade" your array to an arraylist. Arraylists are more sophisticated arrays that have methods like RemoveAt() and InsertAt():

$array = 1..10
$betterarray = [System.Collections.ArrayList]$array
$betterarray.RemoveAt(4)
$betterarray
$array = $betterarray.toArray()
$array