Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an ArrayList from an Array in PowerShell?

Tags:

I've got a list of files in an array. I want to enumerate those files, and remove specific files from it. Obviously I can't remove items from an array, so I want to use an ArrayList. But the following doesn't work for me:

$temp = Get-ResourceFiles $resourceFiles = New-Object System.Collections.ArrayList($temp) 

Where $temp is an Array.

How can I achieve that?

like image 674
Mark Ingram Avatar asked Mar 06 '09 12:03

Mark Ingram


People also ask

Can you convert array to ArrayList?

We can convert an array to arraylist using following ways. Using Arrays. asList() method - Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.

How do you declare an ArrayList in PowerShell?

What is PowerShell Arraylist. We can use an ArrayList to store a list of items in PowerShell. Unlike array, arraylist's 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.

How do I create a list in PowerShell?

To create an array of a specific type, use a strongly typed collection: PS > $list = New-Object Collections. Generic. List[Int] PS > $list.

How do I use an array in PowerShell?

To create and initialize an array, assign multiple values to a variable. The values stored in the array are delimited with a comma and separated from the variable name by the assignment operator ( = ). The comma can also be used to initialize a single item array by placing the comma before the single item.


2 Answers

I can't get that constructor to work either. This however seems to work:

# $temp = Get-ResourceFiles $resourceFiles = New-Object System.Collections.ArrayList($null) $resourceFiles.AddRange($temp) 

You can also pass an integer in the constructor to set an initial capacity.

What do you mean when you say you want to enumerate the files? Why can't you just filter the wanted values into a fresh array?

Edit:

It seems that you can use the array constructor like this:

$resourceFiles = New-Object System.Collections.ArrayList(,$someArray) 

Note the comma. I believe what is happening is that when you call a .NET method, you always pass parameters as an array. PowerShell unpacks that array and passes it to the method as separate parameters. In this case, we don't want PowerShell to unpack the array; we want to pass the array as a single unit. Now, the comma operator creates arrays. So PowerShell unpacks the array, then we create the array again with the comma operator. I think that is what is going on.

like image 79
dan-gph Avatar answered Oct 04 '22 15:10

dan-gph


Probably the shortest version:

[System.Collections.ArrayList]$someArray 

It is also faster because it does not call relatively expensive New-Object.

like image 31
Roman Kuzmin Avatar answered Oct 04 '22 13:10

Roman Kuzmin