Table of Contents
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:
1 2 3 4 5 6 7 8 |
$colours = @("Red", "Green", "Blue") $arrayList = [System.Collections.ArrayList]$colours Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } |
1 2 3 4 5 6 |
ArrayList Contents: Red Green Blue |
Using New ArrayList
Object with AddRange() method
To convert array to ArrayList in PowerShell:
- Create new object of type
System.Collections.ArrayList
usingNew-Object
cmdlet. - Use
AddRange()
method to add array$colours
to$arrayList
object.
1 2 3 4 5 6 7 8 9 |
$colours = @("Red", "Green", "Blue") $arrayList = New-Object System.Collections.ArrayList $arrayList.AddRange($colours) Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } |
1 2 3 4 5 6 |
ArrayList Contents: Red Green Blue |
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.ArrayList
line 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.
Further reading:
Once you have an ArrayList
object, you can use its methods and properties to manipulate the data. These methods are as follows:
- Add an Element
- Remove an Element
- Sort the Elements
- 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.
1 2 3 4 5 6 7 8 9 10 |
$colours = @("Red", "Green", "Blue") $arrayList = New-Object System.Collections.ArrayList $arrayList.AddRange($colours) $arrayList.Add("Yellow") Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } |
1 2 3 4 5 6 7 |
ArrayList Contents: Red Green Blue Yellow |
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.
1 2 3 4 5 6 7 8 9 10 |
$colours = @("Red", "Green", "Blue", "Yellow") $arrayList = New-Object System.Collections.ArrayList $arrayList.AddRange($colours) $arrayList.Remove("Blue") Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } |
1 2 3 4 5 6 |
ArrayList Contents: Red Green Yellow |
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.
1 2 3 4 5 6 7 8 9 10 |
$colours = @("Red", "Green", "Yellow") $arrayList = New-Object System.Collections.ArrayList $arrayList.AddRange($colours) $arrayList.Sort() Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } |
1 2 3 4 5 6 |
ArrayList Contents: Green Red Yellow |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
$colours = @("Red", "Green", "Yellow") $arrayList = New-Object System.Collections.ArrayList $arrayList.AddRange($colours) $count = $arrayList.Count Write-Host "ArrayList Contents:" foreach ($colour in $arrayList) { Write-Host $colour } Write-Host "The total number of elements in the ArrayList = $count" |
1 2 3 4 5 6 7 |
ArrayList Contents: Red Green Yellow The total number of elements in the ArrayList = 3 |
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.