Adding arrays to arraylists

I have an object which is of type {system.collections.arraylist]
To add a single element I can use the Add method but to add an array I have to use the ‘+=’ method.
Does anyone have a technical explanation for this as ‘+=’ doesnt seem to be in the microsoft doco below:

I am also assuming that ‘+=’ does not destroy and rebuild the arraylist everytime an array is added?

I’m not sure if I get what you mean. What about …

[system.collections.arraylist]$ArrayList = 1,2,3,4,5
[array]$Array = 'a','b','c'
$ArrayList.Add($Array)

?

BTW: You should not use ArrayLists anymore …

You should use lists instead:

Thanks. I should have been clearer. I was talking about arrays of objects something like this:

$al = [system.collections.arraylist]@()
$adds = [system.collections.arraylist]@()
$adds.add([pscustomobject]@{
    abc = 1
    xyz = 3
    }) | out-null

$adds.add([pscustomobject]@{
    abc = 2
    xyz = 4
    }) | out-null
$al.add($adds)

I expect $al.count to have a value of 2 but it has one. And if you try and export-csv on $al you get junk.
However instead of $al.add($adds) I do $al += $adds its all fine.
So my concern was do I lose the benefit of arraylists in not destroying/creating a new array every time i ‘+=’ into it?
And just curious - why shouldnt one use arraylists any more? There is something better? btw Im stuck with PS 5.1 if the new thing is version 6 or 7.

$al = [system.collections.arraylist]@()
$adds = [system.collections.arraylist]@()

… both arraylists empty …

$adds.add([pscustomobject]@{
    abc = 1
    xyz = 3
    }) 

… arraylist $adds now contains one element

$adds.add([pscustomobject]@{
    abc = 2
    xyz = 4
    })

… arraylist $adds now contains two elements

now you add the arraylist $adds which is one element to the arraylist $al

$al.add($adds)

… arraylist $al now contains one element … the arraylist $adds.

If you want to count the elements of the contained arraylist $adds you can use

$al[0].count

Because MSFT recommends it. Read the Remark info box I linked. :point_up_2:t4:

It is also in the Remarks info box AND I posted a link to the help topic about … [System.Collections.Generic.List]

What problem are you actually trying to solve with it? There might be an easier way.

Thanks. Ill have a look at that class.
Also found this class - system.collections.generic.hashset. Never heard of it. Is it like @{}?

My logic is something like this:
Initialise my output array.
Run through a loop.
on each loop initialise an internal array and populate it.
At the end of each loop iteration check a criteria to determine whether the internal array is added to the output array.
at the vey end of the script export-csv the output array.

system.collections.generic.dictionary looks more like @{}
Have to spend some time with this generic class it seems.

Most of the times thats unnecessary.

Why does it have to be a newly initialized array in each iteration?

Why would you have doubts about that it gets added if you have code in place to add it?

Do you have some specific code you can share? It sounds a little bit like you overcomplicate your task.

1 Like

Why does it have to be a newly initialized array in each iteration?

because the array may or may not be added to the final array and the internal array only contains info relevant to that iteration of the loop.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.