is there a way to supress the return value (=Index) of an ArrayList in Powershell (using System.Collections.ArrayList) or should I use another class?
$myArrayList = New-Object System.Collections.ArrayList($null) $myArrayList.Add("test") Output: 0
The java. util. ArrayList. add(int index, E elemen) method inserts the specified element E at the specified position in this list.It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
This problem has two typical causes: Static fields used by the objects you stored in the list. Accidentally adding the same object to the list.
To add value to the array, you need to create a new copy of the array and add value to it. To do so, you simply need to use += operator. For example, you have an existing array as given below. To add value “Hello” to the array, we will use += sign.
You can cast to void to ignore the return value from the Add method:
[void]$myArrayList.Add("test")
Another option is to redirect to $null:
$myArrayList.Add("test") > $null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With