The operation of inserting, deleting and adding array elements in PowerShell is very inconvenient and inefficient. That's because the array object itself does not have the ability to insert and delete, and each operation is to copy the entire array into a new array. This process consumes resources too much.
If we convert the array object to a ArrayList object, all the problems are solved. ArrayList has the InsertAt () and RemoveAt () methods, so it is more convenient and efficient to handle the insertion and deletion of array elements.
$array = 1..10
[System.Collections.ArrayList] $arraylist = $array
$arraylist. RemoveAt (4)
$null = $arraylist. ADD (one)
$arraylist. Insert (0, ' new element ')
$arraylist
Explain this piece of code.
1, defines an array, contains 1 to 10, altogether 10 array elements.
2, assign the $array array variable to the $arraylist variable, so that the array into ArrayList
3, respectively executes RemoveAt (), Add (), Insert () and other operations
It is important to remember that this is done not just for the sake of writing code, it will lead to a de facto efficiency boost.