PowerShell use ArrayList to implement array inserts, deletes, add examples _powershell

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.