System Collections ArrayList

In this tutorial, we will discuss, what is a PowerShell ArrayList how to create and use ArrayList.

Also, we will see how to sort an ArrayList, remove items from an ArrayList, how to sort an ArrayList, and how to create an ArrayList from an array.

In my previous article, we discussed what is an array in PowerShell and how to create a PowerShell array from the CSV file.

What is PowerShell Arraylist

We can use an ArrayList to store a list of items in PowerShell.

Unlike array, arraylists length is not fixed, it can changed.

One difference between array and ArrayList is, An array is strongly types, that means array can store only specific type elements. On the other hand, ArrayList can store all the datatype values.

Create ArrayList in PowerShell

Now, we will see how to create an arraylist in PowerShell.

ArrayList is part of the System.Collections namespace within .NET. And we can create a new object of type System.Collections.ArrayList to create an ArrayList.

Below are the two ways to create an arraylist.

$demoarrayList = New-Object -TypeName 'System.Collections.ArrayList'; or $demoarrayList = [System.Collections.ArrayList]::new[]

This way, we can create arraylist of objects type.

You can see by calling the GetType[] method.

How to create an empty ArrayList

We can also create an empty arraylist like below:

[System.Collections.ArrayList]$demoArrayList= @[]

This will create an empty arraylist.

PowerShell ArrayList add

Now, we will see how to add item to arraylist.

We can use the Add[] method to add an item to the arraylist.

$demoarrayList = New-Object -TypeName 'System.Collections.ArrayList'; $demoarrayList.Add["Bijay"] $demoarrayList.Add["Bhawana"] $demoarrayList.Add[100]

You can retrieve the arraylist elements by using the arraylist name like below:

$demoarrayList

How to Get ArrayList Item count

We can count the items from the ArrayList by using the Count property.

$demoarrayList.Count

You can see the output like below:

Sort ArrayList Items

We can easily sort arraylist using sort-object.

$demoarrayList | sort-object

You can see now the Arraylist is displaying in sorted order like below:

This is how we can sort a ArrayList.

PowerShell arraylist foreach

Now, we will see how to iterate ArrayList by using foreach.

Syntax:

foreach [item in itemCollection]{ item }

We can use ArrayList foreach like below:

foreach [$demoitem in $demoarrayList]{ $demoitem }

It will looks like below:

How to create an ArrayList from an Array in PowerShell?

Now, we will see how to create an arraylist from an array.

Below are two ways we can create an arraylist from an array.

$X=2,4,6,8,9,20,5 [System.Collections.ArrayList]$X$X=2,4,6,8,9,20,5 $y=[System.Collections.ArrayList]$X

You can see the output like below:

Below is also another way, we can create an arraylist from an array by using the below script:

$X=2,4,6,8,9,20,5 $y= New-Object System.Collections.ArrayList[,$X]

remove item from ArrayList PowerShell

Now, we will see how to remove item from arraylist.

We can use the RemoveAt[] method to remove an item from the ArrayList.

$X=2,4,6,8,9,20,5 $y=[System.Collections.ArrayList]$X $y.RemoveAt[2]

Remove multiple item from arraylist

Now, we will see how to remove multiple items from PowerShell ArrayList by using the RemoveRange[] method.

$X=2,4,6,8,9,20,5 $y=[System.Collections.ArrayList]$X $y.RemoveRange[1,2]

RemoveRange takes two parameter, first parameter is Index and second parameter is Count.

The above code remove 2 items starting from index 1. So it will remove 4,6 from the arraylist.

This is how we can remove multiple items from an arraylist.

You may like following tutorials:

In this tutorial we learned what is an arraylist and below things:

  • How to Create an ArrayList
  • Create empty ArrayList
  • Add an item to ArrayList
  • Get ArrayList item count
  • Sort an ArrayList
  • Get all items from ArrayList using foreach
  • How to create an ArrayList from an Array
  • Remove items from ArrayList
  • Remove multiple items from ArrayList

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services [SharePoint] MVP [5 times]. I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site EnjoySharePoint.com

Video liên quan

Chủ Đề