2

I have a block which parses a list of files from S3 to get the file names.

$GetFileNamesByType = {
    Param(
        $fileType,
        $list
    )

    $entryList = New-Object System.Collections.ArrayList
    foreach($item in $list){
        if($item.Name.Contains($fileType)){
            $entryList.Add($item.Name) | Out-Null
        }
    }

    return $entryList
}

I call this method and it returns me the following list:

2016-08-31_21-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-30_21-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-30_22-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-30_20-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-31_23-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-31_20-15-17_server-1.1.20558_client-1.1.20518.zip
2016-08-30_23-15-17_server-1.1.20558_client-1.1.20518.zip

Howerver, what I don't understand is that when I check the list type, it is an array instead of ArrayList.

5
  • 1
    Possible duplicate of Function not returning expected object Commented Nov 7, 2016 at 22:48
  • You can use foreach as an expression to generate a normal array faster than ArrayList: return foreach ($item in $list) { if($item.Name.Contains($fileType)){$item.Name} } Also, return is optional. Commented Nov 8, 2016 at 3:11
  • @wOxxOm Thanks for the suggestion, the reason I am using ArrayList is that I need to remove the elements in it dynamically. Maybe I should try LinkedList but if it still returns Array, I have to find a workaround. Commented Nov 9, 2016 at 14:41
  • Convert the normal array produced via foreach expression to arraylist in the end: [Collections.ArrayList]$entryList and it'll be still faster! Well, not by much though. Commented Nov 9, 2016 at 14:50
  • @wOxxOm ah, thanks!! It didn't occur me before :) Commented Nov 9, 2016 at 17:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.