Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent ArrayList.Add() from returning the index

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 
like image 315
Milde Avatar asked Jan 27 '10 17:01

Milde


People also ask

How do you add an element to an ArrayList at a specific index?

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).

Why does my ArrayList contain N copies of the last item added to the list?

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.

How do I add values to an array in Powershell?

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.


1 Answers

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 
like image 151
Keith Hill Avatar answered Sep 18 '22 17:09

Keith Hill