Convert Array to ArrayList in PowerShell

Convert Array to ArrayList in PowerShell

One of the everyday tasks is converting an array to an ArrayList in PowerShell. It can be helpful when you need to work with dynamic data structures or manipulate data in memory. However, before we dive into how to convert an array to ArrayList, it’s essential to understand the difference between these two.

An array is a collection of values having the same type stored in a single variable. Arrays are fixed in size, meaning that once an array is created, its size cannot be changed.

An ArrayList, on the other hand, is a dynamic collection of values of any type that can be resized as needed. It can be helpful when adding or removing elements from a collection.

Now that you know arrays and ArrayList, let’s continue learning various approaches to convert arrays to ArrayList in PowerShell.

Using casting to System.Collections.ArrayList

Cast array object to System.Collections.ArrayList to convert array to ArrayList in PowerShell.
Here is an example:

Using New ArrayList Object with AddRange() method

To convert array to ArrayList in PowerShell:

  • Create new object of type System.Collections.ArrayList using New-Object cmdlet.
  • Use AddRange() method to add array $colours to $arrayList object.

In the above code, the $colours = @("Red", "Green", "Blue") line creates an array of colours and assigns it to the variable $colours. The @() notation creates an array in PowerShell.

The$arrayList = New-Object System.Collections.ArrayListline creates a new ArrayList object and assigns it to the variable $arrayList. The New-Object is used to create a new instance of a .NET class.

After that$arrayList.AddRange($colours) line adds the contents of the $colours array to the $arrayList ArrayList object using the AddRange() method. This method adds multiple items to the ArrayList at once.

The Write-Host cmdlets output a string ArrayList Contents: to the console to indicate that the following lines will output the contents of the ArrayList.

Finally, we used the foreach loop to iterate over each item in the $arrayList ArrayList object and output it to the console using the Write-Host cmdlet. The loop variable $colour stores the current item being processed.

Once you have an ArrayList object, you can use its methods and properties to manipulate the data. These methods are as follows:

  1. Add an Element
  2. Remove an Element
  3. Sort the Elements
  4. Get a Count of Elements

Let’s continue learning these methods.

Use Add() Method of ArrayList Object

Using the Add() to add a new element to ArrayList.

The above code fence is similar to the previous one but added the add() method to insert an element to ArrayList.

Use Remove() Method of ArrayList Object

Use the Remove() method to remove an existing element from an ArrayList.

The above code fence is similar to the previous example, but it uses the Remove() method to eliminate an element from the ArrayList and outputs the updated contents of the ArrayList to the console using the Write-Host cmdlet.

Use Sort() Method of ArrayList Object

Use the Sort() method to sort elements in an ArrayList.

The code above follows a similar pattern as the previous two examples, but in this case, the Sort() method is employed to arrange the items in the ArrayList in ascending order. The Write-Host cmdlet is then used to print the updated contents of the ArrayList to the console.

Use Count Property of ArrayList Object

Use the count property to retrieve the count of total elements present in ArrayList.

This code snippet above follows the same pattern as the previous three examples, but in this case, it uses the Count property to retrieve the total number of items in the ArrayList. The Write-Host cmdlet is then used to display the updated contents on the console.

Based on the solutions above, the ArrayList object can easily be converted from an array to an ArrayList, and its methods and properties can be used to manipulate the data. For example, we add an element to the ArrayList using the Add() method, remove the first element with the Remove() method, sort the elements using the Sort() method, and get the element count with the Count property.

That’s all about how to convert array to ArrayList in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *